Draft proposal: allow the use of relational operators on Comparable classes (original) (raw)
Jeremy Manson jeremy.manson at gmail.com
Tue Mar 10 09:21:24 PDT 2009
- Previous message: Draft proposal: allow the use of relational operators on Comparable classes
- Next message: Draft proposal: allow the use of relational operators on Comparable classes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
My major concerns with this is how consistent it is with the rest of the system:
There is no guarantee that if a < b and b < c, then a < c. This is not true in general in Java.
It is inconsistent to have a < b call compareTo(), but to have == not call equals().
If a < b means compareTo, and a == b means reference equality, then you can't specify reasonable behavior for <= and >=.
To fix this, we'd have to go back in time and have == mean object equality instead of reference equality. That would be nice, but we can't do it.
Jeremy
On Tue, Mar 10, 2009 at 9:09 AM, Vilya Harvey <vilya.harvey at gmail.com> wrote:
I've attached a draft of a proposal to allow classes which implement the Comparable interface to be used as operands for the relational operators. So for example if you had two Strings, a and b, you would be able to write
if (a < b) { ... } instead of if (a.compareTo(b) < 0) { ... } and you could do the same with your own classes as well. Thanks in advance for any feedback, Vil.
- Previous message: Draft proposal: allow the use of relational operators on Comparable classes
- Next message: Draft proposal: allow the use of relational operators on Comparable classes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]