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

Steve Horne stephenwantshornenospam100 at aol.com
Sat Sep 9 03:21:52 PDT 2006


On Fri, 08 Sep 2006 13:06:43 -0700, Kirk McDonald
<kirklin.mcdonald at gmail.com> wrote:
>I think that Object's opEquals should simply call opCmp.
>
>class Object {
>     // ...
>     int opEquals(Object o) {
>         return this.opCmp(o);
>     }
>     // ...
>}

I think you mean...

  return (this.opCmp(o) == 0);

>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.

The problem is, every class already has an opEquals which it inherits
from Object.

Also, it's often good to make it explicit that you really intend
something, rather than to have it just magically happen.

-- 
Remove 'wants' and 'nospam' from e-mail.



More information about the Digitalmars-d mailing list