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: make interfaces holding nil pointers evaluate to NaN (Not a Nil) #56364

Closed
robpike opened this issue Oct 21, 2022 · 1 comment
Closed

Comments

@robpike
Copy link
Contributor

robpike commented Oct 21, 2022

There are two major problems writing Go programs that cause frequent confusion: The so called "nil error" problem (https://go.dev/doc/faq#nil_error), wherein a nil pointer stored in an interface is not equal to nil, even though it seems like it should be; and NaNs going into maps, which complicate matters because they never compare true to one another, violating a number of invariants around map behavior.

I suggest we make both these problems the same, by making an interface holding a nil pointer evaluate to NaN, thus never comparing true and reducing the confusion to a single problem rather than two. To make this easy to understand, we rename the NaN that results be a "not a nil" value, thereby both clarifying and confounding the NaN problem that plagues our world.

@gopherbot gopherbot added this to the Proposal milestone Oct 21, 2022
@hajimehoshi
Copy link
Member

hajimehoshi commented Oct 21, 2022

I'm not sure I understand this suggestion, but an interface with a nil pointer is comparable, while NaN (not a number) is not, right?

https://go.dev/play/p/mldzOxNjqpu

package main

import (
	"fmt"
	"math"
)

func main() {
	var a any = (*int)(nil)
	var b any = (*int)(nil)
	fmt.Println(a == b) // true

	c := math.NaN()
	d := math.NaN()
	fmt.Println(c == d) // false
}

@robpike robpike closed this as completed Oct 21, 2022
@golang golang locked and limited conversation to collaborators Oct 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants