Draft proposal: allow the use of relational operators on Comparable classes

Jeremy Manson jeremy.manson at gmail.com
Tue Mar 10 09:21:24 PDT 2009


My major concerns with this is how consistent it is with the rest of the system:

1) There is no guarantee that if a < b and b < c, then a < c.  This is
not true in general in Java.

2) It is inconsistent to have a < b call compareTo(), but to have ==
not call equals().

3) 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.
>
>
>
>



More information about the coin-dev mailing list