FAQ
I have a collection of structs that use majority non-pointer methods, but
there are a few that would be best coded with pointer receivers. I would
like to bring these structs together into an interface who's qualifications
contain both nonpointer and pointer methods. However, so far as I know,
that isn't possible in practice, is it? After all, an Object and a *Object
are not the same type.

My question is this: What is considered the best way to get around my
conundrum? Should I just make all my methods have pointer receivers, even
if it's not necessary, or is there a better solution? Any advice is
appreciated!

--
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

  • Jesse McNelis at Dec 6, 2013 at 2:22 am

    On Fri, Dec 6, 2013 at 12:36 PM, Tyler Compton wrote:
    I have a collection of structs that use majority non-pointer methods, but
    there are a few that would be best coded with pointer receivers. I would
    like to bring these structs together into an interface who's qualifications
    contain both nonpointer and pointer methods. However, so far as I know, that
    isn't possible in practice, is it? After all, an Object and a *Object are
    not the same type.
    It's generally a good idea to have all the methods for a certain type
    to have pointer receivers or value receivers not both.
    There is little reason to have both and it does get in the way of
    implementing interfaces.
    My question is this: What is considered the best way to get around my
    conundrum? Should I just make all my methods have pointer receivers, even if
    it's not necessary, or is there a better solution? Any advice is
    appreciated!
    They should just be consistent for each type. Feel free to have some
    types have all pointer receivers and some all have value receivers.


    --
    =====================
    http://jessta.id.au

    --
    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.
  • Jesse McNelis at Dec 6, 2013 at 2:27 am

    On Fri, Dec 6, 2013 at 1:22 PM, Jesse McNelis wrote:
    My question is this: What is considered the best way to get around my
    conundrum? Should I just make all my methods have pointer receivers, even if
    it's not necessary, or is there a better solution? Any advice is
    appreciated!
    They should just be consistent for each type. Feel free to have some
    types have all pointer receivers and some all have value receivers.
    Sorry, scrap that.
    The method set of a pointer to a type has all the methods with pointer
    or value receivers, but the value of the type only has the value
    receiver methods.
    So the requirement isn't the method receivers, but whether the value
    your wrapping in an interface is a pointer or a value.
    http://golang.org/ref/spec#Method_sets

    --
    =====================
    http://jessta.id.au

    --
    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.
  • Kevin Gillette at Dec 6, 2013 at 3:25 am
    The reason that viewpoint (either all value receivers, or all pointer
    receivers) is generally accepted is that it avoids subtle, confusing errors
    in which a type in value context may satisfy some, but not all of the
    interfaces that the same type in an addressable/addressed context would.
    The same goes for the availability of direct method calls in
    non-addressable contexts -- it's more clear what's going on when all method
    call sites result in compilation errors rather than when only some of the
    call sites result in errors.
    On Thursday, December 5, 2013 7:22:49 PM UTC-7, Jesse McNelis wrote:
    On Fri, Dec 6, 2013 at 12:36 PM, Tyler Compton wrote:
    I have a collection of structs that use majority non-pointer methods, but
    there are a few that would be best coded with pointer receivers. I would
    like to bring these structs together into an interface who's
    qualifications
    contain both nonpointer and pointer methods. However, so far as I know, that
    isn't possible in practice, is it? After all, an Object and a *Object are
    not the same type.
    It's generally a good idea to have all the methods for a certain type
    to have pointer receivers or value receivers not both.
    There is little reason to have both and it does get in the way of
    implementing interfaces.
    My question is this: What is considered the best way to get around my
    conundrum? Should I just make all my methods have pointer receivers, even if
    it's not necessary, or is there a better solution? Any advice is
    appreciated!
    They should just be consistent for each type. Feel free to have some
    types have all pointer receivers and some all have value receivers.


    --
    =====================
    http://jessta.id.au
    --
    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.
  • Tyler Compton at Dec 10, 2013 at 4:13 am
    Alright, these are all good things to know. Thank you for the help.
    On Thursday, December 5, 2013 6:36:15 PM UTC-7, Tyler Compton wrote:

    I have a collection of structs that use majority non-pointer methods, but
    there are a few that would be best coded with pointer receivers. I would
    like to bring these structs together into an interface who's qualifications
    contain both nonpointer and pointer methods. However, so far as I know,
    that isn't possible in practice, is it? After all, an Object and a *Object
    are not the same type.

    My question is this: What is considered the best way to get around my
    conundrum? Should I just make all my methods have pointer receivers, even
    if it's not necessary, or is there a better solution? Any advice is
    appreciated!
    --
    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
postedDec 6, '13 at 1:36a
activeDec 10, '13 at 4:13a
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase