how to make '==' safe for classes?

Stanislav Blinov stanislav.blinov at gmail.com
Sun Oct 28 18:00:06 UTC 2018


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.

> 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