A fresh look at comparisons

Janice Caron caron800 at googlemail.com
Mon Apr 14 05:01:07 PDT 2008


On 14/04/2008, Henning Hasemann <hhasemann at web.de> wrote:
>                         // Objects are simply incomparable
>                         // (like (1 - i) and (i - 1) for example)
>                         return ComparsionResult.UNDEFINED;

This is worth spending a bit of time discussing. The fact is that, in
D, that particular comparison is not undefined. In fact, it is /very
well/ defined. It is defined as follows:

   cdouble a = 1 - 1i;
   cdouble b = 1i - 1;

   (a == b)  ==  false
   (a < b)  ==  false
   (a <= b)  ==  false
   (a <> b)  ==  false
   (a <>= b)  ==  false
   (a > b)  ==  false
   (a >= b)  ==  false
   (a != b)  ==  true
   (a !<> b)  ==  true
   (a !<>= b)  ==  true
   (a !< b)  ==  true
   (a !<= b)  ==  true
   (a !> b)  ==  true
   (a !>= b)  ==  true

It would be very bad indeed for D, if less-than suddenly became a
tristate operation! In my opinion, we really don't want that. So, we
don't want to be returning UNDEFINED - instead, we want to be defining
the result of each of the fourteen possible comparisons so that each
gives a purley boolean result, and that unorderedness is indicated by
patterns such as the above. My suggestion does give us that.



More information about the Digitalmars-d mailing list