Wrong selection of opEquals for objects.

Alexandru Ermicioi alexandru.ermicioi at gmail.com
Fri Aug 28 10:42:09 UTC 2020


On Friday, 28 August 2020 at 10:28:07 UTC, Simen Kjærås wrote:

> What you'll need to do is mark every function that does compare 
> two class objects with == as @trusted or @system.

No that is not a solution at all, in template code that requires 
safety. You basically will have to sacrifice safety for rest of 
types, such as structs, unions & enums for the sake of objects 
being able to compare.

Could we just template that opEquals in this manner:
-------
bool opEquals(T : Object, X : Object)(T lhs, X rhs)
{
     if (lhs is rhs) return true;

     if (lhs is null || rhs is null) return false;

     if (!lhs.opEquals(rhs)) return false;

     if (typeid(lhs) is typeid(rhs) ||
         !__ctfe && typeid(lhs).opEquals(typeid(rhs)))
     {
         return true;
     }

     return rhs.opEquals(lhs);
}
-------

That would at least allow us to define an overload which is safe 
and would be picked up by new implementation.

I'm wondering why it wasn't done yet, are there any reasons for 
that?

- Alex.



More information about the Digitalmars-d-learn mailing list