Relevant discussion on the Rust subreddit: http://www.reddit.com/r/rust/comments/2xu8rn/boolean_operators_to_not_return_bools/
TL;DR: I’d like to see the boolean operators currently defined by
Ord be made available to return non-booleans. I think this includes all of
>=. I would expect
Ord to retain their current semantics, but I’m not sure if that is possible to do in a backward-compatible way.
In Python, the boolean operators can be overloaded to return any type. It’s more than a bit convoluted how it’s done, with
__radd__, etc to make it all work, but it makes some particularly nice-looking APIs. Take an example from usage of the SQL Alchemy ORM:
MyModel.query.filter(MyModel.my_field < 42).first()
If Python enforced that
MyModel.my_field < 42 had to return a
bool, this would not be possible. Of course, since Python doesn’t enforce any return types, it doesn’t, but it’s clear that this polymorphism is desired in other operators, such as
The way I envision using these operators is by creating traits for each of those operators (I don’t know that forcing them to be implemented together is strictly necessary in all cases). For the sake of discussion perhaps they could be called
Le. If I had all my druthers, I might instead rename
Equiv, and use
Ne to match their method names, but I suspect that’s even less possible than the rest of this proposal.
Then I’d like to see
Ord simply be one possible concrete abstraction over a usage pattern for these operators. I suspect that there are other legitimate reasons for wanting to overload these operators than just an ORM, though my reasoning includes only that use-case and my desire for having operators in general work the same across the board (like
Because these are fundamental types and operators that I’m proposing changing, this may be too little too late and too close to 1.0, but I would be remiss to not bring it up for discussion.