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: go/types: add StrictlyComparable function #72059

Open
kwjw opened this issue Mar 1, 2025 · 4 comments
Open

proposal: go/types: add StrictlyComparable function #72059

kwjw opened this issue Mar 1, 2025 · 4 comments
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Milestone

Comments

@kwjw
Copy link

kwjw commented Mar 1, 2025

Proposal Details

Background

From #56773:

Generics introduced the constraint comparable which denotes the set of all types which are strictly comparable. An API may define an exported type T which may be strictly comparable (see #56548 for terminology) and such a type T may successfully be used as type argument for comparable type parameters elsewhere.

It's possible to change T without visible API change so that it's not strictly comparable anymore.

Whether an exported type is strictly comparable should be considered a part of its API. As such, it would be useful if this property were exposed through go/types.

Determining whether a type is spec-comparable is exposed via types.Comparable:

// Comparable reports whether values of type T are comparable.
func Comparable(T Type) bool {
return comparableType(T, true, nil, nil)
}

In the call comparableType(T, true, nil, nil), the boolean parameter indicates spec-comparability (true). Setting this parameter to false indicates strict comparability.

Proposal

Add the following function to go/types:

// StrictlyComparable reports whether values of type T are strictly comparable
// (https://go.dev/ref/spec#Comparison_operators).
func StrictlyComparable(T Type) bool {
	return comparableType(T, false, nil, nil)
}
@kwjw kwjw added the Proposal label Mar 1, 2025
@gopherbot gopherbot added this to the Proposal milestone Mar 1, 2025
@gabyhelp gabyhelp added the LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool label Mar 1, 2025
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Mar 3, 2025
@ianlancetaylor
Copy link
Member

CC @griesemer @adonovan @findleyr

@kwjw
Copy link
Author

kwjw commented Mar 3, 2025

CC @jba

apidiff could make use of a function like StrictlyComparable. If a previously spec-comparable struct is changed to no longer be comparable, apidiff reports this as an incompatible change. There should be a similar rule for maintaining strict comparability.

@findleyr
Copy link
Member

findleyr commented Mar 3, 2025

I agree that we should add this. In principle, we should support any type predicate that appears in the spec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LibraryProposal Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool Proposal
Projects
Status: Incoming
Development

No branches or pull requests

5 participants