Thread overview
General comparsion operator
Dec 10, 2005
Chris Sauls
Dec 12, 2005
Lionello Lunesu
December 10, 2005
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
December 12, 2005
Could basically be the same as operator !=, no? What if that one returned a different non-zero value for < and > ?

L.


December 12, 2005
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