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

Reinier Zwitserloot reinier at zwitserloot.com
Tue Mar 10 17:03:53 PDT 2009


Reply inline

  --Reinier Zwitserloot



On Mar 10, 2009, at 17:21, Neal Gafter wrote:

> This proposal is incompatible with the existing behavior of the
> comparison operators on the boxed value classes, particularly for
> Float and Double.
>

I'll help Neal out and be a bit more verbose:

Existing behaviour unboxes first, which also means that you get type  
promotion to make parts fit. You can do this:

Float x = 10.0;
Double y = 12.0;

if ( x < y ) /* stuff */;

but you can not call x.compareTo(y), or y.compareTo(x). (AFAIK - it  
would be nice if someone tests this theory. Don't have a javac  
available at the moment).

Easily fixed in the proposal by stating: If the operation would  
succeed under old unboxing rules, use those.

After all, there are ambiguous rules regarding == as well (if I == two  
autoboxed values, is it reference comparison, or an equals check)? A  
choice was made there too.

> 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