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
}
Proposal Details
Background
From #56773:
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:go/src/go/types/predicates.go
Lines 152 to 155 in 6e676ab
In the call
comparableType(T, true, nil, nil), the boolean parameter indicates spec-comparability (true). Setting this parameter tofalseindicates strict comparability.Proposal
Add the following function to
go/types: