opEquals should default to opCmp (was: Re: Associative arrays in D and default comparators)

Kirk McDonald kirklin.mcdonald at gmail.com
Fri Sep 8 13:06:43 PDT 2006

Walter Bright wrote:
> Although the current AA implementation only uses opCmp, having both 
> available means that an implementation could use both or either. 
> Sometimes opEquals can be computed a lot more efficiently than opCmp.

This brings up something that has always annoyed me about 
opCmp/opEquals. While it is true that sometimes opEquals can be computed 
more efficiently than opCmp, this is not always true. Sometimes there is 
only one operation for comparison that makes sense.

I think that Object's opEquals should simply call opCmp.

class Object {
     // ...
     int opEquals(Object o) {
         return this.opCmp(o);
     // ...

This way, a class can simply overload opCmp to overload all comparison 
operations. If it can more efficiently overload opEquals, then it is 
free to do so.

Kirk McDonald
Pyd: Wrapping Python with D

More information about the Digitalmars-d mailing list