Re: [go-nuts] Strange behavior of range over composite literal.

2018-10-08 Thread Raffaele Sena
Thanks for the answers. I tried to read the specs, but didn't go as far as
the parsing ambiguity section :(

-- Raffaele

On Mon, Oct 8, 2018 at 12:20 PM Ian Lance Taylor  wrote:

> On Mon, Oct 8, 2018 at 12:10 PM, Raffaele Sena  wrote:
> >
> > I found a strange behavior (well, I would call it a bug) of for-range
> over a
> > composite literal where the literal type is a user type (or alias). This
> is
> > true for both maps and array/slices, but the following example is with a
> > slice.
>
> For future notice, when asking about a strange behavior, it's always
> extremely helpful to say what the strange behavior is.  There are many
> reasons why we might not see the same behavior.
>
> In this case, search for "parsing ambiguity" in
> https://golang.org/ref/spec#Composite_literals .
>
> Ian
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Strange behavior of range over composite literal.

2018-10-08 Thread Ian Lance Taylor
On Mon, Oct 8, 2018 at 12:10 PM, Raffaele Sena  wrote:
>
> I found a strange behavior (well, I would call it a bug) of for-range over a
> composite literal where the literal type is a user type (or alias). This is
> true for both maps and array/slices, but the following example is with a
> slice.

For future notice, when asking about a strange behavior, it's always
extremely helpful to say what the strange behavior is.  There are many
reasons why we might not see the same behavior.

In this case, search for "parsing ambiguity" in
https://golang.org/ref/spec#Composite_literals .

Ian

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Strange behavior of range over composite literal.

2018-10-08 Thread Jan Mercl
On Mon, Oct 8, 2018 at 9:10 PM Raffaele Sena  wrote:

See https://golang.org/ref/spec#Composite_literals

A parsing ambiguity arises when a composite literal using the TypeName form
of the LiteralType appears as an operand between the keyword
 and the opening brace of the block
of an "if", "for", or "switch" statement, and the composite literal is not
enclosed in parentheses, square brackets, or curly braces. In this rare
case, the opening brace of the literal is erroneously parsed as the one
introducing the block of statements. To resolve the ambiguity, the
composite literal must appear within parentheses.

if x == (T{a,b,c}[i]) { … }
if (x == T{a,b,c}[i]) { … }


-- 

-j

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.