logoalt Hacker News

amiga386today at 8:36 AM1 replyview on HN

Go has had this behaviour for promoting fields (provided they don't clash) for some time, this is just extending the language feature to initialisers.

Your contrived example doesn't initialise the embedded struct that also contains a "Burrow" field. If it did that at all, even without naming the Burrow field... you would not be allowed to initialise the struct, because of the ambiguity.

https://go.dev/ref/spec#Composite_literals

> A key must not denote a promoted field inside an embedded struct if that struct is also specified by another key.

> Given the declarations

    type Object  struct { name, color string }
    type Point3D struct { Object; x, y, z float64 }
    type Line    struct { Object; p, q Point3D }
> .... field selectors may not denote overlapping fields:

    obj   := Object{"edge", "black"}
    line3 := Line{Object: obj, name: "diagonal"} // invalid: name denotes a field inside Object

Replies

pjmlptoday at 9:16 AM

Which error?

https://go.dev/play/p/CFWVXkFBOEX

Note that I added a name field to Line.

    Data:  {edge black}  --  {{edge black} {{ } 0 0 0} {{ } 0 0 0} diagonal}
Oops now Object.name is empty, which is my point.
show 1 reply