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
Language specification: incorrect statement about method sets of pointer types #27786
Comments
The type X in your example (T in your prose) is not allowed to be a pointer type. That is, you cannot define methods on X if X is a pointer. As https://golang.org/ref/spec#Method_declarations says, the receiver type "must not be a pointer or interface type". |
I must respectfully disagree. Eliding a bit of text, the "Method sets" section says
where "other" means "non-interface". In my example, X has a method foo defined on it, so foo is in the method set of X. Applying the above statement with T = X, then foo is in the method set of *X. Applying the statement again with T = *X, then foo is in the method set of **X, which is wrong. Perhaps "any other type T" might be changed to "any other defined type T" or "any other named type T"? In any case, this isn't terribly important to me. If you don't wish to change anything, I won't object. |
The situation cannot arise. Try compiling the code at https://play.golang.org/p/4fWjEy73OTP 👍
There can never be a method on a If you can create a working example of the problem you are trying to show, please do so. |
I understand this statement, and I agree it correctly describes the language. My problem is that, in my reading of Method sets, it implies that there can be methods on a type **X. So it seems to me this section is not a correct description of the language. By the nature of this problem, I can't produce an example that compiles. But please consider (https://play.golang.org/p/bsKB0B9jTDe).
This example contradicts the statement under Method sets that "the method set of ... *T ... contains the method set of T". True, in the example T is not a defined type, but I don't see anything in Method sets that requires T to be a defined type. |
I think the spec is fine. You can't determine the full behavior of a feature from a sentence taken in isolation; it's part of a larger document. So I don't think anything's missing here, but I'll defer to @griesemer |
The (almost) full text about method sets is
It starts with "all methods declared with receiver type T". It is not possible to have a receiver type T where T is a defined pointer type per the explanations about receivers in the method declaration section. Your most recent example uses a type alias (type T = *X) which is why the example works (at least for method f). I agree with you that it's not completely trivial to cobble the various rules about method sets together (and it gets more complicate with embedded types in structs), and at some point it would be nice to clean this up a bit, but I also agree with @robpike that the spec is correct here. Leaving this closed for now. |
The language specification, under Method sets says
The text in parentheses seems to be incorrect if T itself is a pointer type. After
the method sets of
X
and*X
includefoo
, but that of**X
does not.(https://play.golang.org/p/8fvILyZeP1K)
The text was updated successfully, but these errors were encountered: