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

cmpopts: SortSlice s and SortMaps should accept func(T,T) int signature #365

Closed
dsnet opened this issue Sep 27, 2024 · 0 comments · Fixed by #367
Closed

cmpopts: SortSlice s and SortMaps should accept func(T,T) int signature #365

dsnet opened this issue Sep 27, 2024 · 0 comments · Fixed by #367

Comments

@dsnet
Copy link
Collaborator

dsnet commented Sep 27, 2024

The stdlib is gradually coalescing around func(T, T) int over func(T, T) bool as the function to compare elements. See slices.CompareFunc for example.

For better or worse, the cmpopts.SortSlices and cmpopts.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

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
@neild neild closed this as completed in 391980c Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant