-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: src/sort: Expand sort.Sort() and sort.Stable() to use generic types #67861
Comments
It's unclear how the sort package would produce these keys of generic values to pass to the |
This is possible but it would require using new names in the The |
Similar Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
Right, this might have to be implemented as part of an
|
I don't see how that signature works. Where is the result of the sort? Given that under the hood it would have to use a slice anyway, I don't see the problem with:
And maybe |
The sort would occur in-place using the pointer returned by the iterator. This would cause the actual data to be sorted, not just as a copy of a slice. |
That's how C++ iterators work. That is not how Go iterators work. |
You caught me 😆 I guess my question is why not include that functionality if iterators which return by address are already being considered? |
I don't think there are currently any proposals for return-by-address iterators. |
Proposal Details
This proposal is for expanding the sort package in the stdlib to use generics. The Sort and Slices packages have been updated to use generics to cover all other functions in the sort package, but calling Sort() on an arbitrary data structure still requires that the provided
Interface
implementsLess(i, j int) bool
andSwap(i, j int)
instead of allowing for generics. This would be helpful for cases where the primary keys/values of an ordered data structure are not integers.Example:
./prog.go:46:12: cannot use CD (variable of type *CustomData) as sort.Interface value in argument to sort.Sort: *CustomData does not implement sort.Interface (wrong type for method Less) have Less(string, string) bool want Less(int, int) bool
It looks like this wouldn't be too difficult to implement since the sort package already has some generated code for generics. I also didn't find other issues mentioning this proposal but may be missing something obvious.
The text was updated successfully, but these errors were encountered: