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

proposal: spec: use zero receiver for value method invoked via nil pointer #18775

Open
rogpeppe opened this issue Jan 24, 2017 · 4 comments
Open

proposal: spec: use zero receiver for value method invoked via nil pointer #18775

rogpeppe opened this issue Jan 24, 2017 · 4 comments

Comments

@rogpeppe
Copy link
Contributor

@rogpeppe rogpeppe commented Jan 24, 2017

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.

@go101
Copy link

@go101 go101 commented Jan 25, 2017

Why? Looks not reasonable.

@mvdan
Copy link
Member

@mvdan mvdan commented Jan 25, 2017

@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?

@gopherbot gopherbot added this to the Proposal milestone Mar 20, 2017
@gopherbot gopherbot added the Proposal label Mar 20, 2017
@rsc rsc changed the title proposal: make value method work when called on nil pointer proposal: spec: make value method use zero receiver when called on nil pointer Jun 16, 2017
@rsc rsc changed the title proposal: spec: make value method use zero receiver when called on nil pointer proposal: spec: use zero receiver for value method invoked via nil pointer Jun 16, 2017
@bcmills
Copy link
Member

@bcmills bcmills commented Nov 16, 2018

this breaks code that currently relies on catching this panic (notably the
fmt package)

See also #28150.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Feb 12, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.