On Mon, Feb 5, 2018 at 10:42 AM, theckman via golang-nuts
<golang-nuts@googlegroups.com> wrote:
> Hey Gophers,
>
> I've run in to an interesting case regarding Method Sets that makes me
> wonder whether it should be disallowed by the spec[1]. Before opening an
> issue on GitHub I thought I'd reach out here to get some clarifications as I
> may be misreading the spec.
>
> Based on my understanding of method sets, a variable with the type *T should
> have a method set that contains methods on T and *T, while a variable of
> type T should only have methods with a receiver for T in the method set. To
> me this translates that if my variable is not a pointer type it should not
> be able to call a method that's receiving a pointer type.


I believe this is not what the spec says. If type T has:

func (t T) A() {}
func (t *T) B() {}

Then the method set of T is [A], where the method set of *T is [A,B]

For a variable x:=T{}, x.A() is called with a copy of x, and x.B() is
called with a copy of &x. So, for instance:

func f(m map[string]T) {
   m["key"].A() // works
   m["key"].B() // doesn't work, m["key"] is not addressable

>
> However, I've observed that to not be the case. If I have a variable of type
> T, I am able to invoke methods that are receiving *T[2]. Should that be
> permitted by the spec? It adds some inconsistency because I am able to
> invoke the method on my variable, but the variable won't satisfy an
> interface that has that method included[3]. I think it's acceptable that the
> interface isn't satisfied, but what makes it so that invoking the method
> directly is permitted?
>
> Cheers!
> -Tim
>
> [1] https://golang.org/ref/spec#Method_sets
> [2] https://play.golang.org/p/-OMAnD7U0Vl
> [3] https://play.golang.org/p/uP0_eMYqYEe
>
> --
> 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.

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