Making `object.opEquals' replaceable

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri May 8 10:33:31 PDT 2015


On Friday, 8 May 2015 at 17:00:00 UTC, Manfred Nowak wrote:
> Jonathan M Davis wrote:
>
>> And why not just use another function instead of opEquals for 
>> what you want?
>
> Because the local `opEquals' _is_ the overload-function for 
> `==' and `!='.
>
> I guess that your "another function"-opinion holds for every 
> form of overloading.

In general, D is far more restrictive in how it defines 
overloaded operators than C++ is. This helps enforce the 
correctness and consistency of such operators and reduces how 
much code has to be written. Occasionally, that means that there 
are things that you can do in C++ with overloaded operators that 
you cannot do in D with overloaded operators, and perhaps that is 
a loss, but on the whole, it's well worth the gains (e.g. by 
defining only opEquals and opCmp, you get the whole suite of 
overloaded operators, whereas in C++, you'd have to implement 
each and every one of them individually, which is both far more 
verbose and far more error-prone).

So, if an overloaded operator does not work with what you're 
trying to do, then you need to use a function of your own to 
implement it.

- Jonathan M Davis


More information about the Digitalmars-d mailing list