Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
spec: in array and slice literals, the index/key can be non-integer type #23529
What version of Go are you using (
At the beginning of September 2016, the spec simply said
Which was vague but -as I read it- allowed things like
This seems more "strict", in the sense that it explicitly says that typed constants need to be of integer type, and looking at the spec, the part about constant declarations:
it seems to me that a constant declared with type specifier (as
So it seems that the new spec phrasing introduced in September 2016 made the rules more strict, but the compiler was not updated accordingly.
changed the title from
spec: In array and slice literals, the index/key can be non-integer type
spec: in array and slice literals, the index/key can be non-integer type
Jan 24, 2018
This seems to me like an oversight (== bug) in the compiler. The index key is supposed to follow the exact same rules as an index for a slice/array. It's not valid to use a float64 index
go/types complains in all these cases.
@griesemer @rsc go/types misses the case where the index to an array/slice is a typed constant with an underlying type int - same with composite array/slice literals. This should not work according to the spec:
type foo float64
@ianlancetaylor maybe I am confused or reading the spec wrong
bar's type is foo, foo's underlying type is int but the spec says for array literals:
"An element with a key uses the key as its index. The key must be a non-negative constant representable by a value of type int; and if it is typed it must be of integer type."
In this case the key is typed - the type is not 'int' it's foo but foo's underlying type is int. I guess it depends on what "integer type" means. Does it mean just int or anything that has int as the underlying type?
@ak2196 "integer type" does not mean just