WAT: opCmp and opEquals woes

via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 28 04:19:10 PDT 2014


On Monday, 28 July 2014 at 09:37:27 UTC, Regan Heath wrote:
> My point was that for the vast majority of coders, in the vast 
> majority of cases opCmp()==0 will agree with opEquals().  It is 
> only in very niche cases i.e. where partial ordering is 
> actually present and important, that this assumption should be 
> broken.
>
> Yet, by default, if a user defines opCmp() the compiler 
> generated opEquals may well violate that assumption.  This is 
> surprising and will lead to subtle bugs.

The cheap non-breaking solution is to just add opCmpTotal() and 
map opCmp() to that. If opCmpTotal is defined then you cannot 
define opCmp() and opCmp(a,b)==0 should match a==b whether 
redefined or not.


More information about the Digitalmars-d mailing list