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

Open
rogpeppe opened this Issue Jan 24, 2017 · 2 comments

Comments

Projects
None yet
5 participants
@rogpeppe
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.

Show comment
Hide comment
@go101

go101 Jan 25, 2017

Why? Looks not reasonable.

go101 commented Jan 25, 2017

Why? Looks not reasonable.

@mvdan

This comment has been minimized.

Show comment
Hide comment
@mvdan

mvdan Jan 25, 2017

Member

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

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 from proposal: make value method work when called on nil pointer to proposal: spec: make value method use zero receiver when called on nil pointer Jun 16, 2017

@rsc rsc changed the title from proposal: spec: make value method use zero receiver when called on nil pointer to proposal: spec: use zero receiver for value method invoked via nil pointer Jun 16, 2017

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