interface Foo
{
}
class Bar : Foo
{
}
class Baz : Bar, Foo
{
}
void main()
{
Baz baz = new Baz();
Bar bar = baz;
Foo foo1 = bar;
Foo foo2 = baz;
assert(foo1 is foo2);
}
foo1 and foo2 have the same type and point to the same object yet they have different addresses.
The test above passes for C# (http://ideone.com/xK5Mu) and C++ (http://ideone.com/MnnL8 virtual inheritance used, fails otherwise, of course).
Comment #1 by verylonglogin.reg — 2013-12-04T08:52:35Z
*** Issue 11178 has been marked as a duplicate of this issue. ***
Comment #2 by verylonglogin.reg — 2013-12-04T09:07:22Z
Currently Identity Expression over two `interface`s just compare pointers and over an `interface` and a `class` it adds a constant dependent on operands CT types to `class` reference and then compare pointers:
---
interface I {}
class A: I {}
class B: A, I {}
void main() @safe
{
B b = new B;
A a = b;
I ia = a, ib = b;
assert(ia is a); // ok
assert(ib is b); // ok
assert(ia is b); // fails
assert(ib is a); // fails
assert(ia is ib); // fails
}
---
Thus Identity Expression has nothing to do with "the object references are for the same object" except the case of two classes unless this bug is fixed.
Please confirm this bug is fixable.
Anyway if it isn't going to be fixed in the immediate future Identity Expression over `interface`s should be appropriately documented and possibly deprecated.
Comment #3 by verylonglogin.reg — 2013-12-04T09:12:45Z
Filed documentation Issue 11683.
Comment #4 by b2.temp — 2020-09-06T09:07:25Z
D is is not the same as c# and others. On reference types this is more to perform a simple address comparison, exp when opEquals is overloaded.
The assert should be rather
assert(typeid(foo1) is typeid(foo2))
but unfortunately typeid() on interfaces currently has a embarrassing bug, it returns a TypeInfo_Class, not a TypeInfo_Interface.
Comment #5 by default_357-line — 2020-09-06T10:31:30Z
No this is a genuine bug. See https://dlang.org/spec/expression.html#identity_expressions
"For class objects, identity is defined as the object references are for the same object."
I ia and ib are interface references, but they are the same object.
D must transform the interfaces into their class reference before comparing, even if the type is the same; else, multiple implementation of the same interface must be either merged or made illegal.
Comment #6 by robert.schadek — 2024-12-13T17:53:34Z