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 · 3 comments

Comments

Projects
None yet
6 participants
@rogpeppe
Copy link
Contributor

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

This comment has been minimized.

Copy link

go101 commented Jan 25, 2017

Why? Looks not reasonable.

@mvdan

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

bcmills commented Nov 16, 2018

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

See also #28150.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment