www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - TypeInfo of interfaces bugg or feature?

reply "TheFlyingFiddle" <kurtyan student.chalmers.se> writes:
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 { }

    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 ????????
Dec 20 2013
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Friday, 20 December 2013 at 21:21:16 UTC, TheFlyingFiddle 
 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)
Dec 20 2013