Skip to content

proposal: allow to omit types in more contexts #56761

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

Closed
eliasdaler opened this issue Nov 16, 2022 · 1 comment
Closed

proposal: allow to omit types in more contexts #56761

eliasdaler opened this issue Nov 16, 2022 · 1 comment

Comments

@eliasdaler
Copy link

Currently, we can omit types inside slice initialization and map initialization, for example:

m := []map[string]string{{"hello" : "goodbye" }}
m2 := []T{{Field:42 }} // type T struct { Field int }

This can be allowed in more contexts.


1. Allow to omit types in composite literal

Before:

type N struct {
    X int
}
type T struct {
    Items map[int]int
    Val N
}

t := T{
    Items: map[int]int{3: 4},
    Val: N{ X : 42 },
}

After:

type T struct {
    Items map[int]int
}

t := T{
    Items: {3: 4},
    Val: { X : 42 },
}

2. Allow to omit return types inside "return" statement.

Before:

func createMap() map[string]string {
    return map[string]string{
        "hello": "goodbye",
        "a": "b",
    }
}

After:

func createMap() map[string]string {
    return {
        "hello": "goodbye",
        "a": "b",
    }
}

This change will make composite literals easier to write for types containing structs, slices and maps. It doesn't seem to require any language changes and doesn't seem to break previously valid Go code.

The change can be argued with on the premise of "reducing readability", but consider the counter example:

t := &T{
    Items: createMap(), // no type in sight
}

However, this might allow to write shorter code because you'll avoid repeating types. The refactoring of struct member types might also become easier as you won't need to change types inside composite literals.

This doesn't seem to require any language/spec changes and doesn't seem to break previously valid Go code (please feel free to correct me if I'm wrong).

@gopherbot gopherbot added this to the Proposal milestone Nov 16, 2022
@seankhliao
Copy link
Member

Duplicate of #21496

@seankhliao seankhliao marked this as a duplicate of #21496 Nov 16, 2022
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Nov 16, 2022
@golang golang locked and limited conversation to collaborators Nov 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants