interface opEquals
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Thu Nov 23 21:52:56 UTC 2023
On Thursday, November 23, 2023 2:20:25 PM MST Antonio via Digitalmars-d-learn
wrote:
> * Why, when applied to interface, ```opEquals``` called directly
> behavior is not the same that when calling ```==``` ?
>
> * Is it the expected behaviour?
I'd have to take the time to study your code in detail to see whether what
exactly you're seeing makes sense or not, but it's not expected that normal
D code will call opEquals directly, and for classes, == does more than call
lhs.opEquals(rhs). It does additional stuff to try to have the correct
behavior for equality without you having to code it all up yourself in
opEquals.
== on classes results in the free function, opEquals, in object.d being
called. That function does a variety of checks such as checking whether
either reference is null (to avoid dereferencing null) and using is to
compare the address of the class references first (to avoid calling the
class' opEquals if both references are to the same object). It also makes
sure that both rhs.opEquals(lhs) and lhs.opEquals(rhs) are true for == to be
true to avoid subtle bugs that can come into play when comparing a base
class against a derived class.
https://github.com/dlang/dmd/blob/master/druntime/src/object.d#L269
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list