-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: spec: use zero receiver for value method invoked via nil pointer #18775
Comments
Why? Looks not reasonable. |
@golang101 this way you can handle a nil pointer in a single place instead of many. Flipping the question - what makes this not reasonable for a potential Go 2? |
See also #28150. |
If we adopt this it would change the behavior of existing code. That is, the exact same code would compile and run before and after making this change to the language, but it would behave differently. That makes it difficult to implement according to the guidelines at https://github.com/golang/proposal/blob/master/design/28221-go2-transitions.md . I'm not sure the benefit of making this change is worth the cost of having the same code behave differently in different versions of Go. |
Thanks, but there isn't enough benefit here to change the behavior of existing code. Closing. |
Currently, if a method is defined on a value type, that method is also
part of the method set of the pointer type. However, if that method
is called on the nil pointer, it panics.
This means there's no way to define a method that works on all possible
values and pointer values for a type.
I propose that when a value method is called on a nil pointer, instead of panicking,
the method is invoked on the zero value for the type.
As this breaks code that currently relies on catching this panic (notably the
fmt package), it is probably sufficiently backwardly incompatible to justify
the Go2 tag.
The text was updated successfully, but these errors were encountered: