Repository navigation
Description
Composite literals construct values for structs, arrays, slices, and maps. They consist of a type followed by a brace-bound list of elements. e.g.,
x := []string{"a", "b", "c"}I propose adding untyped composite literals, which omit the type. Untyped composite literals are assignable to any composite type. They do not have a default type, and it is an error to use one as the right-hand-side of an assignment where the left-hand-side does not have an explicit type specified.
var x []string = {"a", "b", "c"}
var m map[string]int = {"a": 1}
type T struct {
V int
}
var s []*T = {{0}, {1}, {2}}
a := {1, 2, 3} // error: left-hand-type has no type specifiedGo already allows the elision of the type of a composite literal under certain circumstances. This proposal extends that permission to all occasions when the literal type can be derived.
This proposal allows more concise code. Succinctness is a double-edged sword; it may increase or decrease clarity. I believe that the benefits in well-written code outweigh the harm in poorly-written code. We cannot prevent poor programmers from producing unclear code, and should not hamper good programmers in an attempt to do so.
This proposal may slightly simplify the language by removing the rules on when composite literal types may be elided.
Examples
Functions with large parameter lists are frequently written to take a single struct parameter instead. Untyped composite literals allow this pattern without introducing a single-purpose type or repetition.
// Without untyped composite literals...
type FooArgs struct {
A, B, C int
}
func Foo(args FooArgs) { ... }
Foo(FooArgs{A: 1, B: 2, C:3})
// ...or with.
func Foo(args struct {
A, B, C int
}) { ... }
Foo({A: 1, B: 2, C: 3})In general, untyped composite literals can serve as lightweight tuples in a variety of situations:
ch := make(chan struct{
value string
err error
})
ch <- {value: "result"}They also simplify code that returns a zero-valued struct and an error:
return time.Time{}, err
return {}, err // untyped composite literalCode working with protocol buffers frequently constructs large, deeply nested composite literal values. These values frequently have types with long names dictated by the protobuf compiler. Eliding types will make code of this nature easier to write (and, arguably, read).
p.Options = append(p.Options, &foopb.Foo_FrotzOptions_Option{...}
p.Options = append(p.Options, {...}) // untyped composite literalMetadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsAccepted
Activity
adg commentedon Oct 6, 2015
There is some prior art. We actually implemented this (or something very similar) in the lead-up to Go 1.
The spec changes:
https://codereview.appspot.com/5450067/
https://codereview.appspot.com/5449067/
The code changes:
https://codereview.appspot.com/5449071/
https://codereview.appspot.com/5449070/
https://codereview.appspot.com/5448089/
https://codereview.appspot.com/5448088/
There may be other changes that I'm missing. But in the end we abandoned the changes (and reverted the spec changes); it tended to make the code less readable on balance.
bcmills commentedon Oct 6, 2015
I only see one spec change there (the other one you linked is the compiler implementation).
At any rate: "tend[ing] to make less readable on balance" depends a lot on the specific code. Presumably we've learned more about real-world Go usage (including Protocol Buffers and a variety of other nesting data-types) in the time since then - perhaps it's worth revisiting?
(I've badly wanted literals for return values and channel sends on many occasions - they would be particularly useful when a struct is just a named version of "a pair of X and Y" and the field names suffice to fully describe it.)
minux commentedon Oct 6, 2015
neild commentedon Oct 6, 2015
Under this proposal, the following assignments are identical:
var m map[string]int
m = map[string]int{A: 1}
m = {A: 1}
The only difference is that in the latter case, the type of the literal is derived from the RHS of the expression. In both cases, the compiler will interpret A as a variable name.
I would not allow const untyped composite literals; that's a can of worms.
I think untyped composite literals would be too confusing to use (and compile!) if they came with a default type. :)
neild commentedon Oct 6, 2015
On readability:
This would be a significant language change. Go code would become somewhat terser, on balance. In some cases this would lead to less readable code; in others more so.
I feel that the benefits would outweigh the costs, but that's obviously a subjective judgement. In particular, I think that lightweight tuples (as in the above examples) would be a substantial improvement in a number of places.
jimmyfrasche commentedon Oct 6, 2015
I assume that this proposal is simply expanding the places in which you can elide a type literal.
If so, referring to it as untyped composite literals is a bit confusing as untyped has a specific meaning in Go.
It might make more sense to consider each place separately. I don't see much of a point, other than consistency, in allowing
since you could just do
But the rest would certainly cut down on typing and allow nicer APIs in places.
For example,
is arguably clearer than
and the only alternative at present would be
adg commentedon Oct 6, 2015
I agree that the examples look nice, at a glance. But anything can look
nice without context.
To move this proposal forward, one should apply the change to a corpus of
real Go code so that we may observe its benefits and drawbacks in context.
On 7 October 2015 at 09:12, Damien Neil notifications@github.com wrote:
minux commentedon Oct 6, 2015
neild commentedon Oct 6, 2015
To be clear, this proposal does not allow {A: 1} to implicitly become map[string]int{"A":1}.
minux commentedon Oct 6, 2015
bcmills commentedon Oct 6, 2015
In that last example, A is an identifier (i.e. for a string variable or constant) - not a string literal itself.
minux commentedon Oct 6, 2015
did you mean that the code is actually:
const A = "A"
var m map[string]int
m = {A: 1}
Then there are more ambiguity in syntax.
const A = "A"
var x struct { A int }
x = {A: 1}
What does this mean?
Note my concern is that it's possible to assign {A:1} to
vastly different types: map[string]int and struct { A int }
(what about map[interface{}]int and map[struct{string}]int?)
neild commentedon Oct 6, 2015
x = {A: 1}is precisely equivalent tox = T{A: 1}, whereTis the type ofx.minux commentedon Oct 6, 2015
323 remaining items
gopherbot commentedon Sep 25, 2026
Change https://go.dev/cl/838545 mentions this issue:
spec: composite literals may be untypedecryth commentedon Sep 29, 2026
Playing around with this feature, I note that this code doesn't work currently:
Are there any plans to enable this as part of this work, or would this require #61731?
adonovan commentedon Sep 29, 2026
Yes. This issue is the plan. Work is underway.
[Edit: ah, I see you're referring to generics specifically; apologies. I defer to @griesemer for the answer.]
griesemer commentedon Sep 29, 2026
@ecryth The type inference you would like here would require (at least) #61731 . I don't think we have the bandwidth to get to this for 1.28, sadly.
ecryth commentedon Sep 29, 2026
That's unfortunate, that means I won't get much use out of this feature in the code that could use it the most :/
Will keep my hopes up that #61731 will see some traction sooner rather than later, then.
gopherbot commentedon Oct 5, 2026
Change https://go.dev/cl/845526 mentions this issue:
internal/types/testdata/spec: add test cases for untyped composite literalsinternal/types/testdata/spec: add test cases for untyped composite li…
spec: document composite literals with omitted types
feat: support composite literals with inferred types
cherrymui commentedon Nov 10, 2026
Done
dmitshur commentedon Nov 10, 2026
This still needs a release note, right? Not seeing it at https://tip.golang.org/doc/go1.28. Filed a separate release-blocking issue #82973 for that.
gopherbot commentedon Nov 11, 2026
Change https://go.dev/cl/862461 mentions this issue:
doc: document composite literals with omitted types (update release notes)doc: document composite literals with omitted types (update release n…