www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - General comparsion operator

reply Chris Sauls <ibisbasenji gmail.com> writes:
The Ruby language defines a comparison operator "<=>" whose value is a negative
if the 
left-hand operand is "less than" the right-hand operand, positive if the
reverse, or zero 
if they are equivelant.  Fairly straightforward, yes.  Would an equivelant
operator be 
useful in D?  The matter of overloading is taken care of, as it would just
directly use 
the value of .opCmp() since it already follows this convention.  What would
such an 
operator look like in D, I wonder?

cmp = a <=> b ;
cmp = a <-> b ;
cmp = a   b ;
cmp = a >< b ; // I'd rather reserve this one as a swap operator, but that's
another story

-- Chris Sauls
Dec 10 2005
next sibling parent "Lionello Lunesu" <lio remove.lunesu.com> writes:
Could basically be the same as operator !=, no? What if that one returned a 
different non-zero value for < and > ?

L. 
Dec 12 2005
prev sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Chris Sauls wrote:

 The Ruby language defines a comparison operator "<=>" whose value is a 
 negative if the left-hand operand is "less than" the right-hand operand, 
 positive if the reverse, or zero if they are equivelant.  Fairly 
 straightforward, yes.  Would an equivelant operator be useful in D?
+1 I also like the old "starship operator", as Perl fondly calls it. Very useful for writing simpler sorting functions, for instance ?
 What would such an operator look like in D, I wonder?
 
 cmp = a <=> b ;
 cmp = a <-> b ;
 cmp = a   b ;
 cmp = a >< b ; // I'd rather reserve this one as a swap operator, but that's
another story 
If the starship operator *is* added, it should look the same. (<=>) It will blend right in with the D floating point comparison ones: !<>= <> <>= !<= !< !>= !> !<> --anders
Dec 12 2005