Integral overflow, Order relations

Nathan Reed nathaniel.reed at gmail.com
Tue Aug 21 17:34:39 PDT 2007


bearophile wrote:
> Generally on a set you can't define a true opCmp, because sometimes:
> 
> A < B? No.
> A = B? No.
> A > B? No.
> 
> C++ order operators are all separated, like in Python, but D has opCmp that I think can't be used to implement those <= and >= among sets (but it's not a big problem because Python sets can be used by normal English named methods too, so those two operations can be accessed by methods instead of methods and operators).
> Adding a fourth possible return value to opCmp (with meaning "undefined") can solve this problem, allowing to represent partial order relations too.

I'd argue that it's not necessarily appropriate to overload the 
comparison operators in this case, since their semantics (partial 
ordering) don't match the semantics that those operators have with 
numeric types.

For the same reason, you can't use opAdd to write an operation that's 
not commutative, since addition is commutative with numeric types.

Thanks,
Nathan Reed



More information about the Digitalmars-d mailing list