how to make '==' safe for classes?

ikod geller.garry at gmail.com
Sun Oct 28 19:20:30 UTC 2018


Thanks for excellent explanation!

On Sunday, 28 October 2018 at 19:00:52 UTC, Jonathan M Davis 
wrote:
>
> Because Object predats @safe (and most attributes), it really 
> isn't compatible with them. In fact, it predates const (since 
> it's basically the same as it was in D1) and it's only possible 
> to compare const class objects because of a hack in the free 
> function opEquals (which == lowers to) which casts away const 
> (meaning that if you're not careful, you can actually violate 
> the type system with == by mutating an object in a class'
...
> So, unfortunately, for now, you're going to have to use 
> @trusted with == on classes, as stupid as that is, though as a 
> workaround, you could always create an @trusted wrapper 
> function that just called ==. It would still be annoying, but 
> it would be cleanly restricted the @trusted bits.
>
> - Jonathan M Davis




More information about the Digitalmars-d-learn mailing list