== operator

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 3 19:37:16 PST 2015


On 1/3/15 7:23 PM, anonymous wrote:
> For reference, here is .object.opEquals (according to documentation[1]):
>
> bool opEquals(Object a, Object b)
> {
>      if (a is b) return true;
>      if (a is null || b is null) return false;
>      if (typeid(a) == typeid(b)) return a.opEquals(b);
>      return a.opEquals(b) && b.opEquals(a);
> }
>
> I see one fundamental source of overhead: The types degenerate to
> Object, resulting in virtual calls that could be avoided. Maybe it'd be
> worthwhile to templatize object.opEquals: `bool opEquals(A, B)(A a, B b)`.

Good point. It's been discussed but rejected because druntime generally 
shuns templates. I think that resistance is mostly vestigial by now.

> Also, the typeid thing could be counter-productive with trivial
> equalities. But it helps with complex ones.
>
> By the way, I think `typeid(a) == typeid(b)` is silly. It calls
> object.opEquals on the `typeid`s. And if they're not identical, that in
> turn calls object.opEquals on the `typeid`s of the `typeid`s. That
> fortunately hits the `is` case, or we'd go on forever. All that only to
> realize that `typeid(a).opEquals(typeid(b))` suffices.
>
> [1] http://dlang.org/operatoroverloading.html

Interesting. Is a pull request in your future? :o) -- Andrei


More information about the Digitalmars-d mailing list