-
Notifications
You must be signed in to change notification settings - Fork 209
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
cmpopts: SortSlice s and SortMaps should accept func(T,T) int signature #365
Comments
dsnet
added a commit
that referenced
this issue
Oct 23, 2024
The SortSlices and SortMaps predate generics and accept an interface{}, so it is possible with reflection to support other function signatures than "func(T, T) bool". In particular, the Go ecosystem is increasingly moving towards "func(T, T) int" as the signature for ordering as evidenced by the newer slices.SortFunc function in stdlib. Thus, modernize cmpopts by supporting "func(T, T) int". Fixes #365
dsnet
added a commit
that referenced
this issue
Oct 23, 2024
The SortSlices and SortMaps options predate generics and accept an interface{}, so it is possible with reflection to support other function signatures than "func(T, T) bool". In particular, the Go ecosystem is increasingly moving towards "func(T, T) int" as the signature for ordering as evidenced by the newer slices.SortFunc function in stdlib. Thus, modernize cmpopts by supporting "func(T, T) int". Also, bump the minimum version to Go 1.21 to match the minimum supported version of google.golang.org/protobuf. Fixes #365
dsnet
added a commit
that referenced
this issue
Oct 23, 2024
The SortSlices and SortMaps options predate generics and accept an interface{}, so it is possible with reflection to support other function signatures than "func(T, T) bool". In particular, the Go ecosystem is increasingly moving towards "func(T, T) int" as the signature for ordering as evidenced by the newer slices.SortFunc function in stdlib. Thus, modernize cmpopts by supporting "func(T, T) int". Also, bump the minimum version to Go 1.21 to match the minimum supported version of google.golang.org/protobuf. Fixes #365
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The stdlib is gradually coalescing around
func(T, T) int
overfunc(T, T) bool
as the function to compare elements. See slices.CompareFunc for example.For better or worse, the
cmpopts.SortSlices
andcmpopts.SortMaps
uses Go reflection to call an arbitrary function (since it predates the introduction of Go generics). One benefit of the non-generic API is that we can support both a "less"-like function and a "compare"-like function.\cc @icio
The text was updated successfully, but these errors were encountered: