Description
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).