how to make '==' safe for classes?

Neia Neutuladh neia at ikeran.org
Sun Oct 28 18:17:41 UTC 2018


On Sun, 28 Oct 2018 18:00:06 +0000, 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:
> ```
> 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.

More pedantically, it rewrites it as:

  (a is b) ||
  (a !is null && (cast(Object)a).opEquals(cast(Object)b))

An object might not be equal to itself via opEquals, but it will always 
compare equal to itself with ==.


More information about the Digitalmars-d-learn mailing list