Type inference inside struct literals? Pretty Please?

141 views
Skip to first unread message

Nate Finch

unread,
Sep 12, 2019, 5:02:19 PM9/12/19
to golang-nuts
I wish go's type inference worked inside struct literals. Filling out config structs would be so much easier if this worked:

type Config struct {
 Runtime struct {
    Debug bool
 }
}
c := Config{
 Runtime: {
   Debug: true,
 },
}
^^ why can't this work?  It seems unambiguous.

The initial Config{ defines the type and tells the compiler exactly what everything inside it must be....

The reason that I wrote the struct that way is that it makes it very easy to read when the struct gets bigger. The sub-structs become basically just namespaces on the fields they encompass.  But if you then have to write a literal by redefining the struct, the literal becomes impossible to read


c := Config{
 Runtime: struct { Debug bool }{
   Debug: true,
 },
}




Ian Lance Taylor

unread,
Sep 12, 2019, 6:28:02 PM9/12/19
to Nate Finch, golang-nuts
I think you may be looking for https://golang.org/issue/21496.

Ian

Nate Finch

unread,
Sep 13, 2019, 9:17:59 AM9/13/19
to golang-nuts
Thanks!  Yes, let's do that :)

On Thursday, September 12, 2019 at 6:28:02 PM UTC-4, Ian Lance Taylor wrote:
Reply all
Reply to author
Forward
0 new messages