Skip to content

spec: type inferred composite literals #12854

Closed
1 of 1 issue completed
Closed

Description

@neild

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 specified

Go 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 literal

Code 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 literal

Activity

  1. adg commented on Oct 6, 2015

    @adg
    Contributor

    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.

  2. bcmills commented on Oct 6, 2015

    @bcmills
    Contributor

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

  3. minux commented on Oct 6, 2015

    @minux
    Member
  4. neild commented on Oct 6, 2015

    @neild
    ContributorAuthor

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

  5. neild commented on Oct 6, 2015

    @neild
    ContributorAuthor

    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.

  6. jimmyfrasche commented on Oct 6, 2015

    @jimmyfrasche
    Member

    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

    var t T = {}
    

    since you could just do

    var t = T{}
    

    But the rest would certainly cut down on typing and allow nicer APIs in places.

    For example,

    Polygon({1, 2}, {3, 4}, {5, 4})
    

    is arguably clearer than

    Polygon([]image.Point{{1, 2}, {3, 4}, {5, 4}})
    

    and the only alternative at present would be

    Polygon(image.Point{1, 2}, image.Point{3, 4}, image.Point{5, 4})
    
  7. adg commented on Oct 6, 2015

    @adg
    Contributor

    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:

    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.

    —
    Reply to this email directly or view it on GitHub
    #12854 (comment).

  8. minux commented on Oct 6, 2015

    @minux
    Member
  9. neild commented on Oct 6, 2015

    @neild
    ContributorAuthor

    To be clear, this proposal does not allow {A: 1} to implicitly become map[string]int{"A":1}.

  10. minux commented on Oct 6, 2015

    @minux
    Member
  11. bcmills commented on Oct 6, 2015

    @bcmills
    Contributor

    In that last example, A is an identifier (i.e. for a string variable or constant) - not a string literal itself.

  12. minux commented on Oct 6, 2015

    @minux
    Member

    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?)

  13. neild commented on Oct 6, 2015

    @neild
    ContributorAuthor

    x = {A: 1} is precisely equivalent to x = T{A: 1}, where T is the type of x.

  14. minux commented on Oct 6, 2015

    @minux
    Member
  15. 323 remaining items

  16. gopherbot commented on Sep 25, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/838545 mentions this issue: spec: composite literals may be untyped

  17. ecryth commented on Sep 29, 2026

    @ecryth

    Playing around with this feature, I note that this code doesn't work currently:

    package main
    
    type Foo[T any] struct {
    	A int
    	B T
    }
    
    func main() {
    	hello({
    		A: 42,
    		B: "hi", // cannot use "hi" (untyped string constant) as T value in struct literal
    	})
    }
    
    func hello[T any](f Foo[T]) {}

    Are there any plans to enable this as part of this work, or would this require #61731?

  18. adonovan commented on Sep 29, 2026

    @adonovan
    Member

    Are there any plans to enable this as part of this work

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

  19. griesemer commented on Sep 29, 2026

    @griesemer
    Contributor

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

  20. ecryth commented on Sep 29, 2026

    @ecryth

    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.

  21. gopherbot commented on Oct 5, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/845526 mentions this issue: internal/types/testdata/spec: add test cases for untyped composite literals

  22. cherrymui commented on Nov 10, 2026

    @cherrymui
    Member

    Done

  23. dmitshur commented on Nov 10, 2026

    @dmitshur
    Member

    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.

  24. gopherbot commented on Nov 11, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/862461 mentions this issue: doc: document composite literals with omitted types (update release notes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

  • Status
    Accepted

Relationships

None yet

Development

No branches or pull requests

Issue actions