Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Go 2: obtain length of array from its type #43483

Closed
AndrewWPhillips opened this issue Jan 4, 2021 · 3 comments
Closed

proposal: Go 2: obtain length of array from its type #43483

AndrewWPhillips opened this issue Jan 4, 2021 · 3 comments
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Milestone

Comments

@AndrewWPhillips
Copy link

The len() function built into Go is evaluated at compile-time when it is given an array. The only way I have found to obtain the length of a type parameter (using the go2go tool) is to create a dummy variable of the type before I call len on it.

func f[T interface{ type [2]int }]() {
	var dummy T
	const c = len(dummy)
}

go2go playground

But sometimes you can't declare a dummy variable. In this case it would be useful for the built-in len function to alternatively take a type parameter so I could do the following:

func f[T interface{ type [2]int }]() {
	const c = len(T)  // WARNING: does not currently compile
}

Perhaps a better approach would be to add a variation of len that takes a type parameter using generics syntax.

func f[T interface{ type [2]int }]() {
	const c = len[T]()  // WARNING: does not currently compile
}

Of course, if this was done then it would also make sense to treat the other built-in functions that take a type parameter (new and make) in the same way.

	i := new[int]() // and deprecate i := new(int)
	make[[]int](2)  // and deprecate make([]int, 2)
@AndrewWPhillips AndrewWPhillips changed the title https://go2goplay.golang.org/p/jWBVa0gbIre proposal: Go 2: obtain length of array from its type Jan 4, 2021
@gopherbot gopherbot added this to the Proposal milestone Jan 4, 2021
@tmthrgd
Copy link
Contributor

tmthrgd commented Jan 4, 2021

You can use T{} and thus len(T{}) to avoid having to declare a variable. Take a look: https://go2goplay.golang.org/p/kr8xdGz0Ecn. It seems like that should cover any use case.

@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Jan 4, 2021
@griesemer
Copy link
Contributor

There are two issues here: One is to obtain the length of an array from its array type, and one is to obtain the length of an array that is the single type in a type list of a constraint interface.

Regarding the former; what @tmthrgd said: Given an array type A, since it's an array, it is always valid to create an array literal A{} and then take the length of that len(A{}). Note that the compiler doesn't generate any code for that (per the spec: "The expressions len(s) and cap(s) are constants if the type of s is an array or pointer to an array and the expression s does not contain channel receives or (non-constant) function calls; in this case s is not evaluated.").

Regarding the latter: Given our current interpretation of the generics draft design, A{} happens to be valid for a type parameter A if for all types in A's constraint type the expression A{} is valid and produces the same type. That is, such type lists can only contain a single type. In that case, the len "trick" from above works, but it's definitively stretching the interpretation of type parameters a bit. We probably need to be more precise in an eventuell spec and decide explicitly if this should be valid.

Either way, given that there exist reasonable work-arounds, there's definitely not a strong case here for changing the language.

@AndrewWPhillips
Copy link
Author

Thanks @tmthrgd. I don't know why that did not occur to me.

@golang golang locked and limited conversation to collaborators Jan 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

5 participants