FAQ
http://play.golang.org/p/YcFI_BL12G

var aImpl A = new(AImpl) // Why is this valid ?
var aImplRef *A = new(AImpl) // Why is this invalid ?

Since i'm storing a reference to a pointer, i would expect the second line
to be valid and the first one to be invalid.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dmitry Vyukov at Mar 10, 2013 at 11:10 am

    On Sun, Mar 10, 2013 at 3:00 PM, John Doe wrote:
    http://play.golang.org/p/YcFI_BL12G

    var aImpl A = new(AImpl) // Why is this valid ?
    This is valid bacause *AImpl implements A interface, and that's you
    put into aImpl.
    var aImplRef *A = new(AImpl) // Why is this invalid ?
    This is invalid because you need to assign values of type *A to
    variables of type *A.

    What would work is (though somewhat strange):
    var aImplRef *A = new(A)

    Then you can do:
    *aImplRef = new(AImpl)
    (*aImplRef).Foo()


    Since i'm storing a reference to a pointer, i would expect the second line
    to be valid and the first one to be invalid.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • John Doe at Mar 10, 2013 at 11:22 am
    In case of:

    var aImpl A = new(AImpl)

    Am i correct to assume, that if i were to pass around aImpl variable, it
    would be passed as a pointer to AImpl without having AImpl values copied
    over in a function call ?
    On Sunday, March 10, 2013 1:10:13 PM UTC+2, Dmitry Vyukov wrote:
    On Sun, Mar 10, 2013 at 3:00 PM, John Doe wrote:
    http://play.golang.org/p/YcFI_BL12G

    var aImpl A = new(AImpl) // Why is this valid ?
    This is valid bacause *AImpl implements A interface, and that's you
    put into aImpl.
    var aImplRef *A = new(AImpl) // Why is this invalid ?
    This is invalid because you need to assign values of type *A to
    variables of type *A.

    What would work is (though somewhat strange):
    var aImplRef *A = new(A)

    Then you can do:
    *aImplRef = new(AImpl)
    (*aImplRef).Foo()


    Since i'm storing a reference to a pointer, i would expect the second line
    to be valid and the first one to be invalid.

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dmitry Vyukov at Mar 10, 2013 at 11:27 am

    On Sun, Mar 10, 2013 at 3:22 PM, John Doe wrote:
    In case of:

    var aImpl A = new(AImpl)

    Am i correct to assume, that if i were to pass around aImpl variable, it
    would be passed as a pointer to AImpl without having AImpl values copied
    over in a function call ?

    Yes, that's correct.
    Interface variables are always just 2 words, and only that 2 words are
    copied. So that's cheap.



    On Sunday, March 10, 2013 1:10:13 PM UTC+2, Dmitry Vyukov wrote:
    On Sun, Mar 10, 2013 at 3:00 PM, John Doe wrote:
    http://play.golang.org/p/YcFI_BL12G

    var aImpl A = new(AImpl) // Why is this valid ?
    This is valid bacause *AImpl implements A interface, and that's you
    put into aImpl.
    var aImplRef *A = new(AImpl) // Why is this invalid ?
    This is invalid because you need to assign values of type *A to
    variables of type *A.

    What would work is (though somewhat strange):
    var aImplRef *A = new(A)

    Then you can do:
    *aImplRef = new(AImpl)
    (*aImplRef).Foo()


    Since i'm storing a reference to a pointer, i would expect the second
    line
    to be valid and the first one to be invalid.

    --
    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...@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • John Doe at Mar 10, 2013 at 11:40 am
    Ok, thanks. One last question - if the interface type variable is always
    the same size, does that mean, that assigning it to a pointer is overkill ?

    On Sunday, March 10, 2013 1:27:54 PM UTC+2, Dmitry Vyukov wrote:
    On Sun, Mar 10, 2013 at 3:22 PM, John Doe wrote:
    In case of:

    var aImpl A = new(AImpl)

    Am i correct to assume, that if i were to pass around aImpl variable, it
    would be passed as a pointer to AImpl without having AImpl values copied
    over in a function call ?

    Yes, that's correct.
    Interface variables are always just 2 words, and only that 2 words are
    copied. So that's cheap.



    On Sunday, March 10, 2013 1:10:13 PM UTC+2, Dmitry Vyukov wrote:
    On Sun, Mar 10, 2013 at 3:00 PM, John Doe wrote:
    http://play.golang.org/p/YcFI_BL12G

    var aImpl A = new(AImpl) // Why is this valid ?
    This is valid bacause *AImpl implements A interface, and that's you
    put into aImpl.
    var aImplRef *A = new(AImpl) // Why is this invalid ?
    This is invalid because you need to assign values of type *A to
    variables of type *A.

    What would work is (though somewhat strange):
    var aImplRef *A = new(A)

    Then you can do:
    *aImplRef = new(AImpl)
    (*aImplRef).Foo()


    Since i'm storing a reference to a pointer, i would expect the second
    line
    to be valid and the first one to be invalid.

    --
    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...@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dmitry Vyukov at Mar 10, 2013 at 11:55 am

    On Sun, Mar 10, 2013 at 3:40 PM, John Doe wrote:
    Ok, thanks. One last question - if the interface type variable is always the
    same size, does that mean, that assigning it to a pointer is overkill ?
    It's a bit involved.
    The interface var is 2 words, first work is type descriptor, the
    second word is either pointer to value or the value itself if it fits
    into the word.
    So, if you assign a big struct to interface, it will be stored the
    same way as if you assign pointer to struct.
    However, if you assign a small value that fits into a word, storing it
    directly is more efficient.
    But you get different semantics in that cases, see:
    http://play.golang.org/p/9R7OHyYZOh

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Rémy Oudompheng at Mar 10, 2013 at 3:40 pm

    On 2013/3/10 John Doe wrote:
    In case of:

    var aImpl A = new(AImpl)

    Am i correct to assume, that if i were to pass around aImpl variable, it
    would be passed as a pointer to AImpl without having AImpl values copied
    over in a function call ?
    It's not correct. It is correct that values will not be copied as you
    pass "aimpl" (the interface value) around, but if you call a method
    with value receiver, the value will be copied as argument to the
    method.

    Rémy.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 10, '13 at 11:00a
activeMar 10, '13 at 3:40p
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase