On Sat, Oct 22, 2016 at 8:42 AM, Matt Harden <matt.har...@gmail.com> wrote:
> I don't like the syntax &int{0} because int is not a compound type, but
> &int(0) seems reasonable syntax to me. I do think there is an irregularity
> in the language here:
>
> type Int struct{i int}
> anInt := Int{5}
> p1 := &Int{5}     // works
> p2 := &anInt.i    // works
> p3 := &Int{5}.i   // compiler error "cannot take the address of Int
> literal.i"
>
> So we can take the address of an Int literal (suggesting that struct
> literals are addressable), and we can take the address of a field of an
> addressable struct, but we can't take the address of a field of a struct
> literal?

That is correct.  Struct literals (and composite literals in general)
are not addressable.  The &T{v1, v2} syntax is a special syntactic
sugar meaning to allocate a new instance of T, initialize elements,
and return a pointer to the result.  At one point we considered
changing it to (*T){v1, v2} to emphasize that this is really an
expression that returns a value of type *T, but we decided that &T{v1,
v2} looked better and was clear enough.

You can, of course, write
    p3 := &(&Int{5}).i

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