opCmp with template type not working.

Dominikus Dittes Scherkl dominikus at scherkl.de
Sun Nov 8 12:52:14 UTC 2020


On Wednesday, 4 November 2020 at 15:51:04 UTC, Paul Backus wrote:
> opCmp can return whatever you want, [...]
> IMO there's no reason to use a floating-point number unless you 
> specifically need partial ordering. For the common case of a 
> totally-ordered type, a signed integer is fine.

Yeah, but int is 32 bit, same as float. So why use a type that 
restricts the use cases without any benefit?
Simply always use float, so you have no changes if you later 
detect that your type to compare is in fact _not_ totally ordered 
(e.g. has some special cases). And beliefe me, for most types you 
will earlier or later detect, that they are not totally ordered.
Today when I look, most libraries do some ugly and non-performant 
special casing because their comparison operators didn't handle 
the not-comparable case correctly. In D we can do better, as we 
CAN handle it. So everybody should be taught to use float as the 
result of opCmp. The earlier we do it correct, the better for the 
language!



More information about the Digitalmars-d mailing list