how to make '==' safe for classes?

ikod geller.garry at gmail.com
Sun Oct 28 18:56:10 UTC 2018


On Sunday, 28 October 2018 at 18:00:06 UTC, Stanislav Blinov 
wrote:
> On Sunday, 28 October 2018 at 12:38:12 UTC, ikod wrote:
>
>> and object.opEquals(a,b) do not inherits safety from class C 
>> properties, and also I can't override it.
>
> Yep. Since Object is the base class and it defines opEquals as:

object.opEquals(a,b) even is not a Object member function, it's 
free function.

It looks a bit unnatural for me, but thanks for confirmation!


> ```
> bool opEquals(Object);
> ```
>
> the compiler rewrites `a == b` as 
> `(cast(Object)a).opEquals(cast(Object)ob)`, i.e. it inserts a 
> @system call into your code.
>
>> Is there clean way to use '==' here, or I have to convert this 
>> to a.opEquals(b) for classes, leaving '==' for structs?
>
> Pretty much, yes. "Implicit" value comparison in general is 
> somewhat alien for classes, since they're reference types.




More information about the Digitalmars-d-learn mailing list