WAT: opCmp and opEquals woes

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 25 03:07:35 PDT 2014


On Friday, 25 July 2014 at 08:02:18 UTC, Jacob Carlborg wrote:
> 1. If neither opCmp or opEquals are defined, the compiler will 
> automatically generate these and will be used for comparison 
> and equivalent
>
> 2. If opEquals is defined, lhs == rhs will be lowered to 
> lhs.opEquals(rhs)
>
> 3. If opCmp is defined but no opEquals, lhs == rhs will be 
> lowered to lhs.opCmp(rhs) == 0
>
> 4. If opCmp and opEquals is defined, lhs == rhs will be lowered 
> to lhs.opEquals(rhs)

The compiler _never_ defines opCmp for you. You have to do that 
yourself. So, what you're suggesting would force people to define 
opEquals just because they defined opCmp unless they wanted to 
take a performance hit. And once you define opEquals, you have to 
define toHash. So, what you're suggesting would force a lot more 
code to define toHash, which will likely cause far more bugs than 
simply requiring that the programmer define opEquals if that's 
required in order to make it consistent with opEquals.

- Jonathan M Davis


More information about the Digitalmars-d mailing list