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: clarify rules for uniqueness of method names #14309

Closed
momchil-velikov opened this issue Feb 12, 2016 · 2 comments
Closed

spec: clarify rules for uniqueness of method names #14309

momchil-velikov opened this issue Feb 12, 2016 · 2 comments
Milestone

Comments

@momchil-velikov
Copy link
Contributor

The specification states that

In a method set, each method must have a unique non-blank method name.

It also specifies that promoted methods are included in the method set of a struct:

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.
  • If S contains an anonymous field *T, the method sets of S and *S both include promoted methods with receiver T or *T.

These two excerpts seem to imply that there exist a conflict between a promoted method and a non-promoted method with the same name, or between two promoted methods, like in these examples:

package main
type A struct{}
func (A) F() {}
type B struct{ A }
func (B) F() {}
func main() {}
package main
type A struct{}
func (A) F() {}
type B struct{}
func (B) F() {}
type C struct {
    A
    B
}
func (C) F() {}
func main() {}

However, all of go, gccgo, and gotypes allow the above programs, so perhaps the intent is for the method name to be unique only among the non-promoted methods?

@ianlancetaylor
Copy link
Contributor

The rules are spelled out at https://golang.org/ref/spec#Selectors . But the spec doesn't seem to clearly state that those rules apply not just to selectors, but also to method sets. At least, if it does state that, I can't find it.

@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Feb 12, 2016
@ianlancetaylor
Copy link
Contributor

Oh, wait, I see it now. A field or method is promoted if x.f is valid. I think all is correct, if terse.

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