You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
}
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)
The text was updated successfully, but these errors were encountered:
AndrewWPhillips
changed the title
https://go2goplay.golang.org/p/jWBVa0gbIre
proposal: Go 2: obtain length of array from its type
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.
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.
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 calllen
on it.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:Perhaps a better approach would be to add a variation of
len
that takes a type parameter using generics syntax.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
andmake
) in the same way.The text was updated successfully, but these errors were encountered: