Behavior of opEquals
Jacob Carlborg via Digitalmars-d
digitalmars-d at puremagic.com
Wed Sep 2 11:57:08 PDT 2015
I encountered a problem in the implementation of
std.xml.Document.opEquals (yes, I've reported an issue). The problem is
demonstrated with this example:
class Base
{
int a;
override bool opEquals (Object o)
{
if (auto base = cast(Base) o)
return base.a == a;
else
return false;
}
}
class Foo : Base
{
int b;
override bool opEquals (Object o)
{
if (auto foo = cast(Foo) o)
return super == cast(Base) foo && foo.b == b;
else
return false;
}
}
void main()
{
auto f1 = new Foo;
auto f2 = new Foo;
assert(f1 == f2);
}
This code will result in an infinite recursion. I think the problem is
in the super call, due to == being rewritten to call object.opEquals.
The implementation of object.opEquals will call opEquals on the actual
instances. The call will be dynamically resolved and end up calling
Foo.opEquals instead of Base.opEquals.
Is this really good behavior, something a developer would expect? I
mean, in every other case calling super.someMethod will actually call
the method in the base class.
In this case the solution/workaround is to explicitly call
super.opEquals, but that will miss some optimizations implemented in
object.opEquals.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list