Behavior of opEquals
Jacob Carlborg via Digitalmars-d
digitalmars-d at puremagic.com
Tue Sep 8 23:37:14 PDT 2015
On 2015-09-08 18:40, Jonathan M Davis wrote:
> Well, it might be a bit annoying, but it's simply a matter of adjusting
> your code to call opEquals explicitly when trying to call the base
> version
Given that fact that I found this problem in std.xml.Document shows
either that this is not so easy to remember or that std.xml is basically
never used and it doesn't have a test for this. That it doesn't have a
test is true regardless.
> ,whereas without the free function opEquals, you have subtle
> correctness problems. For instance, if you have base == derived and
> derived == base, you'll get the same result for both for D, whereas the
> equivalent Java or C# could would likely not, because the free function
> opEquals checks both lhs.opEquals(rhs) and rhs.OpEquals(lhs) whether you
> did base == derived or derived == base.
>
> So, while what we have is by no means perfect, I think that it is an
> improvement over what Java and C# did.
It should be possible for the compiler to see that it's a super call and
generate a call to opSuperEquals (or similar) instead, which would make
a non-virtual call when calling opEquals on the instance.
--
/Jacob Carlborg
More information about the Digitalmars-d
mailing list