Skip to content

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

@rogpeppe

Description

@rogpeppe

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.Proposalv2An incompatible library change

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions