-
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: add package 'operators' #58559
Comments
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as resolved.
This comment was marked as resolved.
see also #21498 |
While a package like this could certainly be useful, I think a more general solution, like lightweight functions, would be better overall. |
One generic implementation here: e.g. |
A quick way to turn a generic operator into a concrete function would address this better, agreed. But that requires a language-level change, while this is a library-only solution. But I'd be happy with either. |
I dismissed this proposal at first because I was distracted by the usecase which doesn't strike me as very compelling, but on further reflection, this is a great idea for what to do about math/v2. I think the proposal should be to add a new package called |
Can the title of the issue be changed to "proposal: add package math/ops" or to use mrwonko's original package name "proposal: add package operators"? The current title is a bit misleading. |
Strings can be compared GT, LTE etc. too but i wouldn't call that maths. |
How about package sort/ops? |
Makes sense. Or even just the sort package itself. |
Python's operators package has add, mul, mod, etc. I don't think those really make sense for Go. How about this: package cmp // import "sort/cmp"
func Eq[T comparable](a, b T) bool
func Ne[T comparable](a, b T) bool
type Orderable interface { /* ... */ }
func Gt[T Orderable](a, b T) bool
func Gte[T Orderable](a, b T) bool
func Lt[T Orderable](a, b T) bool
func Lte[T Orderable](a, b T) bool
func Compare[T Orderable](a, b T) int
func Min[T Orderable](a, b T) T
func Max[T Orderable](a, b T) T
func Abs[T Orderable](t T) T Any objections? Additions? Note that Edited. |
Which makes me wonder: As Go doesn't have partial application, would a second set of functions that return another function make sense? Something like |
D'oh, yes, that's a thinko. I somehow got thinking about it as a method. Will edit the list. |
That feels inelegant, a special case solution to a general problem. Maybe lightweight anonymous functions (#21498) could be a better solution for that? Or some kind of |
Note that there is a proposal for variadic generics: #56462. |
How about this then:
|
Edit: |
Another popular name for that kind of function is |
These to/of functions look reqlly ugly to me and I'm not sure there's a much need compared to the plain operators commonly used for sorting. There are other uses for operators as functions but sorting is the major one. We already have Contains in /x/slices, so no need for EqTo in a ContainsFunc. What would you want these partially applied functions for that its worth them being added to the stdlib? |
|
Yeah. I think the main use is to just pass |
New try: package cmp // import "sort/cmp"
func Eq[T comparable](a, b T) bool
func Ne[T comparable](a, b T) bool
type Orderable interface { /* ... */ }
func Gt[T Orderable](a, b T) bool
func Gte[T Orderable](a, b T) bool
func Lt[T Orderable](a, b T) bool
func Lte[T Orderable](a, b T) bool
type Type[T Orderable] struct{ Val T }
// For type inference
func Op[T Orderable](t T) Type[T] { return Type[T]{t} }
func (Type[T]) Eq(other T) bool
func (Type[T]) Ne(other T) bool
func (Type[T]) Gt(other T) bool
func (Type[T]) Gte(other T) bool
func (Type[T]) Lt(other T) bool
func (Type[T]) Lte(other T) bool
func (Type[T]) Compare(other T) int
func Compare[T Orderable](a, b T) int
func Min[T Orderable](val T, vals ...T) T // fixed typo
func Max[T Orderable](val T, vals ...T) T
func Clamp[T Orderable](min, val, max T) T
func Abs[T Orderable](t T) T In another package |
Introducing a type in between is an interesting option. If it wasn't for the fact that it's a wrapper, method expressions and method values would allow that single method to function in either way: partial := Op(a).Lt // func(T) bool
full := Op[T].Lt // func(Type[T], T) bool |
This proposal seems to be motivated by a desire for saving a couple of keystrokes, because seeing the actual operator literally on the page is clearer than hiding it behind a cryptic name.
|
This reeks of worst kitchen sink Java/Python bloat. Support for defining proper definitions for operators on structs as user definable functions would be much better and help solve the |
#59488 has basically obsoleted this proposal. You could try to get more functions added to cmp though. |
This proposal has been added to the active column of the proposals project |
Agreed. Built-in types could have methods for these things, that operations are syntactic sugar for, at least conceptually. int16 could have an Equals(int16) bool method. Generic constraints could describe these methods where appropriate. |
We have several times considered and rejected defining methods for built-in types. That is not this proposal. If you want to discuss that, please do it on a forum or in a different issue. Thanks. |
The stated reason for this proposal is to have comparison functions for sorting purposes. As @carlmjohnson points out, this has been done in the upcoming 1.21 release with the new |
To repeat what Ian said two weeks ago, we've standardized on cmp.Compare for comparison, not less-than functions. It sounds like cmpopts should add a new function that takes a 3-way compare function. |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
When using the
github.com/google/go-cmp/cmp/cmpopts
package, I sometimes find myself writing code likeNow that we have generics, there could be an
operators
package like in Python that provides the operators as functions, likeso that I can write
instead. Or maybe it should be
LT
, given Go capitalises initialisms.The text was updated successfully, but these errors were encountered: