On Fri, Sep 16, 2016 at 3:42 AM, Jan Mercl <0xj...@gmail.com> wrote:
> Consider this program (https://play.golang.org/p/V0fu9rD8_D)
>
>         package main
>
>         import (
>                 "fmt"
>         )
>
>         func main() {
>                 c := make(chan int, 1)
>                 c <- 1
>                 d := <-chan int(c)
>                 fmt.Printf("%T\n", d)
>                 e := (<-chan int)(c)
>                 fmt.Printf("%T\n", e)
>         }
>
> Its output is
>
>         int
>         <-chan int
>
> Intuitively it seems ok (and I believe the output is correct). Let's look
> closer on the RHS expression of line
>
>                 d := <-chan int(c)
>
> From the specs:
>
> Expression = UnaryExpr | Expression binary_op Expression .
> UnaryExpr = PrimaryExpr | unary_op UnaryExpr .
> unary_op = "+" | "-" | "!" | "^" | "*" | "&" | "<-" .
> PrimaryExpr = Operand | Conversion | PrimaryExpr Selector | PrimaryExpr
> Index | PrimaryExpr Slice | PrimaryExpr TypeAssertion | PrimaryExpr
> Arguments .
> Conversion = Type "(" Expression [ "," ] ")" .
> Type = TypeName | TypeLit | "(" Type ")" .
> TypeLit = ArrayType | StructType | PointerType | FunctionType |
> InterfaceType | SliceType | MapType | ChannelType .
> ChannelType = ( "chan" | "chan" "<-" | "<-" "chan" ) ElementType .
>
> It seems to me that parsing `<-chan int(c)` as
>
>         a receive operation from a conversion of `c` to type `chan int`
>
> is as valid as parsing it as
>
>         a conversion of `c` to type `<-chan int`.
>
> In the later case the program would output
>
>         <-chan int
>         <-chan int
>
> I must be missing something. Which rule selects the first parse? Can anybody
> please enlighten me? Thanks in advance.

The rule you are looking for is at
https://golang.org/ref/spec#Conversions : "If the type starts with the
operator * or <-, or if the type starts with the keyword func and has
no result list, it must be parenthesized when necessary to avoid
ambiguity."

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.

Reply via email to