requirement for brackets around immediate struct literal use in condition

53 views
Skip to first unread message

kortschak

unread,
Mar 29, 2012, 1:08:54 AM3/29/12
to golan...@googlegroups.com
This compiles: http://play.golang.org/p/OPE2knxemT

This doesn't: http://play.golang.org/p/9DL08Xw-9q

Am I correct in understanding that this is just a consequence of the lexer rules?

Rob 'Commander' Pike

unread,
Mar 29, 2012, 1:16:58 AM3/29/12
to kortschak, golan...@googlegroups.com

It's not the lexer. The issue is in the spec:

A parsing ambiguity arises when a composite literal using the TypeName
form of the LiteralType appears between the keyword and the opening
brace of the block of an "if", "for", or "switch" statement, because
the braces surrounding the expressions in the literal are confused
with those introducing the block of statements. To resolve the
ambiguity in this rare case, the composite literal must appear within
parentheses.

-rob

Reply all
Reply to author
Forward
0 new messages