Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upproposal: Go 2: spec: always permit comparisons against zero value of type #26842
Comments
jimmyfrasche
added
Go2
NeedsInvestigation
labels
Aug 7, 2018
gopherbot
added this to the Proposal milestone
Aug 7, 2018
gopherbot
added
the
Proposal
label
Aug 7, 2018
ianlancetaylor
changed the title from
proposal: spec: Simplify comparability
to
proposal: Go 2: spec: always permit comparisons against zero value of type
Aug 7, 2018
ianlancetaylor
added
the
LanguageChange
label
Aug 7, 2018
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
cherrymui
Aug 8, 2018
Contributor
Does the zero value need to be static? Or it can be dynamic, as long as at run time, at the point of ==, the value is zero?
From https://play.golang.org/p/JOmxaVJoYtT,
// var zero t
// fmt.Println(a == zero)
it seems to suggest that the zero value does not have to be static (it is a variable). What if the value is not zero, then? Panic?
|
Does the zero value need to be static? Or it can be dynamic, as long as at run time, at the point of ==, the value is zero? From https://play.golang.org/p/JOmxaVJoYtT,
it seems to suggest that the zero value does not have to be static (it is a variable). What if the value is not zero, then? Panic? |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
jimmyfrasche
Aug 8, 2018
Member
One side would need to be known to be zero statically. Having a universal zero like in the linked issue would be the easiest way to achieve that.
|
One side would need to be known to be zero statically. Having a universal zero like in the linked issue would be the easiest way to achieve that. |
jimmyfrasche commentedAug 7, 2018
Go, effectively, has three-kinds of comparability:
==operator defined for a type (structs and arrays with incomparable fields).==operator but it can only test against the zero value (funcs, maps, and slices can be compared againstnil). The spec treats these as incomparable types and notes the special case.==operator and it works for all valuesWhen writing code with known types, this is not an issue. You know if there's an
==operator and whether you can compare against all values or just the zero value.When generating code (or, possibly in the future, writing generic code) over arbitrary types, this asymmetry is a bit more bothersome. You can't just classify types as comparable or incomparable, you need to handle the 0-comparable case as well even though it is so similar: https://play.golang.org/p/JOmxaVJoYtT
I propose collapsing incomparable and 0-comparable. Allow incomparable structs and arrays to be compared to their zero value. There would always be an
==operator and it would always be safe to compare any value to zero. Care would still need to be taken to ensure comparability when using==between arbitrary values, but the simpler classification eases matters.I believe this would be a 100% backwards-compatible change and perhaps even simplify the spec a bit (or at least not require too many changes).
The concerns would be go/types and reflect, but they both seem to lump what I call 0-comparability in with incomparability, but there, admittedly, could be subtler implications.
This is tangentially related to defining a universal zero value #19642 (comment) in that similar arguments are involved and the two would compliment each other.