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: rule about methods declaration #22386

Closed
frojasg opened this issue Oct 22, 2017 · 2 comments
Closed

spec: rule about methods declaration #22386

frojasg opened this issue Oct 22, 2017 · 2 comments

Comments

@frojasg
Copy link
Contributor

frojasg commented Oct 22, 2017

If you write the following method:

func (i int) method() {
	fmt.Printf("number: %d\n", i)
}

You get the following compilation error: cannot define new methods on non-local type.
I looked in the language specification, and I couldn't find any rule regarding where you can't define a method. I was wondering if it's something worth documenting in the spec. Thanks!

p.s. Shouldn't the error message be "cannot define new methods on a non-local type"?

@griesemer
Copy link
Contributor

The spec is clear about this: "The type denoted by T is called the receiver base type; it must not be a pointer or interface type and it must be defined in the same package as the method."
(https://golang.org/ref/spec#Method_declarations)

This is working as intended.

PS: The error message could say "on a non-local type" but what we have is fine and its somewhat customary to be shorter in error messages than in usual prose.

@frojasg
Copy link
Contributor Author

frojasg commented Oct 23, 2017

Thanks!

@golang golang locked and limited conversation to collaborators Oct 23, 2018
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

3 participants