Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spec: Promoted type T gives access to *T receiver. #10581

Closed
egor7 opened this issue Apr 27, 2015 · 3 comments
Closed

spec: Promoted type T gives access to *T receiver. #10581

egor7 opened this issue Apr 27, 2015 · 3 comments

Comments

@egor7
Copy link

egor7 commented Apr 27, 2015

Hi,
I've faced the following misunderstanding. The ref/spec says that:

Given a struct type S and a type named T, promoted methods are included in the method set of the struct as follows:

  • If S contains an anonymous field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.

So I expect to get an error accessing promoted method of S with anonymouse field T with the receiver *T: see playground
But it compiles well and gives the correct answer instead of an error.

@rjeczalik
Copy link

@egor7 Works as expected, the example calls (*S).Val() and not (S).Val(), since it's not l-value. The lack of promotion you can observe with:

https://play.golang.org/p/miJ8-tJWzn

@dominikh
Copy link
Member

That isn't caused by S's method set (c.f. https://play.golang.org/p/HdLASR62Js), but by syntactic sugar:

If x is addressable and &x's method set contains m, x.m() is shorthand for (&x).m()

https://golang.org/ref/spec#Calls

(c.f. https://play.golang.org/p/gxZ8nKOeC-)

@egor7
Copy link
Author

egor7 commented Apr 27, 2015

Thanks.

@egor7 egor7 closed this as completed Apr 27, 2015
@mikioh mikioh changed the title Promoted type T gives access to *T receiver. spec: Promoted type T gives access to *T receiver. Apr 27, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants