December 20, 2013
When using the typeid expression on a instance of an interface it gives the type of the interface not the type of the underlying object. Is this intended or is it a bug?


interface I { }

class A : I { }
class B : A { }

unittest
{
   A a = new A();
   B b = new B();

   writeln(typeid(a)); //Prints A
   writeln(typeid(b)); //Prints B
   a = b;
   writeln(typeid(b)); //Prints B

   I i = b;
   writeln(typeid(i)); //Prints I ????????
}
December 20, 2013
On Friday, 20 December 2013 at 21:21:16 UTC, TheFlyingFiddle wrote:
> Is this intended or is it a bug?

Intended, because an interface is not necessarily an Object. It might also be a COM object or a C++ object, and the typeinfo may not be available for them. (I think, maybe it would be from the .di file at least, but it is a different root anyway).

If you cast to Object then do typeid, you'll get the typeinfo you expect. (or null if it isn't a D Object)