The last bit structural literal stuttering

47 views
Skip to first unread message

Hans Stimer

unread,
Oct 28, 2010, 12:15:02 PM10/28/10
to golang-nuts
If we could get this fixed than we would be another step closer to a
"well-studied mechanical colleague".

type A struct { a int; }
type B struct { A; b string; }

var b = B{
A: A{ // We know we have an A, so why the stutter?
a: 1,
},
b: "b", // Look Ma, no stuttering here!
}

Rob 'Commander' Pike

unread,
Oct 28, 2010, 12:43:04 PM10/28/10
to Hans Stimer, golang-nuts

We aren't allowing struct literals to elide the types, at least not yet, because elision can cause subtle bugs as programs evolve. Unlike in a slice, the fields of a struct can be rearranged, and the types provide more information about correctness than the values themselves. Such rearrangement could introduce subtle bugs that would be hard to discover, especially in a large program. Thus the elision is not permitted for structs at the moment.

-rob

Rob 'Commander' Pike

unread,
Oct 28, 2010, 12:46:46 PM10/28/10
to Hans Stimer, golang-nuts List
... and for the particular case where you need to write A:A for an embedded struct, I agree the stuttering is annoying but the rule would get a little too precise. If we decide to go ahead with structs, the issue would be moot anyway.

-rob

Reply all
Reply to author
Forward
0 new messages