opEquals default behaviour - poorly documented or am I missing something?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 19 00:22:41 PST 2015


On Tuesday, November 17, 2015 20:25:30 user123ABCabc via Digitalmars-d-learn wrote:
> On Tuesday, 17 November 2015 at 19:44:36 UTC, Ali Çehreli wrote:
> >     if (typeid(a) == typeid(b)) return a.opEquals(b);
>
> Wow this is terrible to compare two objects in D. The line I
> quoted means that two TypeInfoClass are likely to be allocated,
> right ?

No. As Steven points out. No allocation takes place.

> But usually when comparing objects one rather cares about the
> reference itself, so a comparison of the two heap addresses is
> enough in this case. (meaning same or not same instance,
> regardless of the their members values).

Really? I would have expected caring about reference equality to be the
_rare_ case rather than the common one. And if that's what you want, ==
isn't the right operator to use anyway. That's what the is operator is for.
Regardless, as the code posted by Ali indicates, the free function opEquals
that gets called by == for classes does check whether they're the same
object first by using the is operator, so all of the other checking is only
done if they're not the same object.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list