proposal: Go 2: spec: basic type inferred slice literals #47709
Labels
FrozenDueToAge
LanguageChange
Proposal
Proposal-FinalCommentPeriod
v2
A language change or incompatible library change
Milestone
Slices can be initialized with a brace-bound, comma separated list of literals along with the type preceding the braces:
Based on the feedback in #12854, I propose users be able to omit the type on slices of basic types as an intermediate step. The code above may then be written as:
It is my belief that issue #12854 is too great of a step in the right direction and the consequences of eliding many types could cause readability issues, as were discussed. I have deliberately excluded map and struct types from this proposal since they were a source of issues. By reducing the cases in which types may be elided we preserve one of the greatest strengths of Go: it's readability.
It is my argument that this does not impact the readability negatively since it only elides basic types. Whenever this syntax is seen in the wild it can easily be deduced to be one of the 6 "inferrable" basic types.
Some possible effects of the change
Casting types with underlying basic type:
Go-time's idea on using
gofmt
to allow users to elide typesA while ago issue #12854 was discussed on the go-time podcast and there was a suggestion type eliding could be used by the programmer and when
gofmt
was run, it would add the type by itself. This could even be used as a fix in case type elision gets out of hand in the future and gophers agree we prefer to read typed composite literals.The text was updated successfully, but these errors were encountered: