Skip to content

proposal: go/types: add StrictlyComparable function #72059

Open
@kwjw

Description

@kwjw

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions