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: spec: basic type inferred slice literals #47709

Closed
soypat opened this issue Aug 15, 2021 · 4 comments
Closed

proposal: Go 2: spec: basic type inferred slice literals #47709

soypat opened this issue Aug 15, 2021 · 4 comments
Labels
Milestone

Comments

@soypat
Copy link

soypat commented Aug 15, 2021

Slices can be initialized with a brace-bound, comma separated list of literals along with the type preceding the braces:

A := []string{"a", "b", "c"}
var Y = []string{"x", "y", "z"}
L := []int{1, 2, 3}

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:

A := {"a", "b", "c"}
var Y = {"x", "y", "z"}
L := {1, 2, 3}
var Y2 []int = {9, 8, 7} // new way of declaring slices

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:

import "example.org/pkg
// ...
type foo []int
c := foo({1, 2, 3})
d := pkg.Bar({"a", "c", "d"})

Go-time's idea on using gofmt to allow users to elide types

A 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.

@gopherbot gopherbot added this to the Proposal milestone Aug 15, 2021
@soypat soypat changed the title proposal: spec: basic slice types inferred literals proposal: spec: basic type inferred slice literals Aug 15, 2021
@ianlancetaylor ianlancetaylor changed the title proposal: spec: basic type inferred slice literals proposal: Go 2: spec: basic type inferred slice literals Aug 15, 2021
@ianlancetaylor ianlancetaylor added v2 A language change or incompatible library change LanguageChange labels Aug 15, 2021
@deanveloper
Copy link

related - #12854

@ianlancetaylor
Copy link
Contributor

Now that we have generics, we could introduce slices.Of which could take an arbitrary list of values (of the same type) and return a slice of that type.

    A := slices.Of("a", "b", "c")

This would work for all kinds of values, not just literals. Admittedly slices.Of is not short, but where it occurs frequently we could use a local function.

@ianlancetaylor
Copy link
Contributor

Based on the discussion above, and the alternative suggested above, this is a likely decline. Leaving open for four weeks for final comments.

(If someone wants to write a separate proposal for slices.Of, that would be welcome.)

@ianlancetaylor
Copy link
Contributor

No further comments.

@golang golang locked and limited conversation to collaborators Nov 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants