www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - typeid of an object whose static type is an interface returns the

reply "Mark Isaacson" <turck11 hotmail.com> writes:
If I have a variable whose static type is an interface and I call
typeid on it, I get the interface back, not the dynamic type.
This seems like confusing behavior. Is this the intended result?

I recognize that one needs some amount of state to perform the
dynamic type lookup, and so it is on that thought that a reason
for this might be based.

My workaround to this issue is shown in the code below, namely,
casting to Object before running typeid produces the expected
result. You can repro the issue by removing that cast.

import std.stdio;
import std.conv;

interface Base {

}

class Derived : Base {

}

void main() {
    Base b = new Derived();
    //1) As is, this prints Derived
    //2) Without the cast, this prints Base -- this is what is
unexpected
    //3) If Base is changed to a class instead of an interface,
this prints Derived regardless of whether or not the cast is in
place
    writeln(text(typeid(cast(Object) b)));
}
Jun 27 2014
parent reply "Kapps" <opantm2+spam gmail.com> writes:
On Friday, 27 June 2014 at 21:23:52 UTC, Mark Isaacson wrote:
 If I have a variable whose static type is an interface and I 
 call
 typeid on it, I get the interface back, not the dynamic type.
 This seems like confusing behavior. Is this the intended result?

 I recognize that one needs some amount of state to perform the
 dynamic type lookup, and so it is on that thought that a reason
 for this might be based.
Interfaces are not necessarily Objects (particularly with the case of IUnkown or extern(C++)), and are handled somewhat differently from objects. When you cast to Object, you're actually subtracting a few bytes from the pointer to get back to the Object, so technically the variable refers not to Object but to the interface. It is a bit odd (along with some of the other side-effects), but it does make some sense since you're referring to the interface and not to an Object. That being said, I'm not 100% sure whether this is the intended behaviour when you actually do point to a class derived from Object.
Jun 28 2014
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 29 Jun 2014 01:07:11 -0400, Kapps <opantm2+spam gmail.com> wrote:

 On Friday, 27 June 2014 at 21:23:52 UTC, Mark Isaacson wrote:
 If I have a variable whose static type is an interface and I call
 typeid on it, I get the interface back, not the dynamic type.
 This seems like confusing behavior. Is this the intended result?

 I recognize that one needs some amount of state to perform the
 dynamic type lookup, and so it is on that thought that a reason
 for this might be based.
Interfaces are not necessarily Objects (particularly with the case of IUnkown or extern(C++)), and are handled somewhat differently from objects.
As I've said many times, instances of D interfaces are necessarily Objects. Any D class that implements a D interface is a D object, and should implicitly cast to Object.
 When you cast to Object, you're actually subtracting a few bytes from  
 the pointer to get back to the Object, so technically the variable  
 refers not to Object but to the interface. It is a bit odd (along with  
 some of the other side-effects), but it does make some sense since  
 you're referring to the interface and not to an Object.
Correct. It would be a trivial extra-step of subtraction to get the TypeInfo of the class itself.
 That being said, I'm not 100% sure whether this is the intended  
 behaviour when you actually do point to a class derived from Object.
It is the intended behavior. And I think your reasoning of the IUnknown problem is the intended reasoning. And I think it's wrong :) -Steve
Jun 29 2014