Thread overview
get actual classinfo through an interface?
Nov 21, 2007
BC
Nov 21, 2007
Christopher Wright
Nov 22, 2007
Robert Fraser
Nov 22, 2007
Christopher Wright
Nov 22, 2007
Ary Borenszweig
Nov 22, 2007
Christopher Wright
Nov 22, 2007
Robert Fraser
November 21, 2007
new question. since IInterface.classinfo returns IInterface, how to get
the actual value of the concrete class?

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
November 21, 2007
BC wrote:
> new question. since IInterface.classinfo returns IInterface, how to get
> the actual value of the concrete class?
> 

Do you have a variable that's passed as IInterface?

---
interface IFoo {}
class Foo : IFoo {}
void whatever (IFoo foo) {
   assert (foo.classinfo is Foo.classinfo);
}
---

That should, I think, work. It fails.

I believe that .classinfo is a static field, which is why it fails. It needs to be accessed in a virtual way. Since typeof() is compile-time, it doesn't help.


You can do the ultra-cruddy way, if you're just looking for equality with some known type, which is naturally completely untested:

bool isType!(T)(Object o) {
   auto vtbl = (*(cast(void*[]*)&o))[0];
   return vtbl == T.classinfo.vtbl.ptr;
}

If you have a classinfo dictionary, that doesn't help, unless you start storing by vtbl ptr instead.

Of course, Walter's free to change the layout of objects any time he wishes, so this is quite version-specific.
November 22, 2007
BC wrote:
> new question. since IInterface.classinfo returns IInterface, how to get
> the actual value of the concrete class?
> 

typeid.

interface Foo { }
class Bar : Foo { }
// ...
Foo foo = new Bar();
TypeInfo ti = typeid(foo);
November 22, 2007
"BC" <NOTmi_emayl_adrez@hotmail.com.remove.not> wrote in message news:op.t15gsgmttn71h3@hal9000xp...
> new question. since IInterface.classinfo returns IInterface, how to get the actual value of the concrete class?
>

Something to keep in mind is that an interface doesn't necessarily point to a D class, which is probably why this information isn't available in the general case.


November 22, 2007
Robert Fraser wrote:
> BC wrote:
>> new question. since IInterface.classinfo returns IInterface, how to get
>> the actual value of the concrete class?
>>
> 
> typeid.
> 
> interface Foo { }
> class Bar : Foo { }
> // ...
> Foo foo = new Bar();
> TypeInfo ti = typeid(foo);

Error: foo is used as a type.
November 22, 2007
Christopher Wright wrote:
> Robert Fraser wrote:
>> BC wrote:
>>> new question. since IInterface.classinfo returns IInterface, how to get
>>> the actual value of the concrete class?
>>>
>>
>> typeid.
>>
>> interface Foo { }
>> class Bar : Foo { }
>> // ...
>> Foo foo = new Bar();
>> TypeInfo ti = typeid(foo);
> 
> Error: foo is used as a type.

Probably

typeid(typeof(foo))
November 22, 2007
Ary Borenszweig wrote:
> Christopher Wright wrote:
>> Robert Fraser wrote:
>>> BC wrote:
>>>> new question. since IInterface.classinfo returns IInterface, how to get
>>>> the actual value of the concrete class?
>>>>
>>>
>>> typeid.
>>>
>>> interface Foo { }
>>> class Bar : Foo { }
>>> // ...
>>> Foo foo = new Bar();
>>> TypeInfo ti = typeid(foo);
>>
>> Error: foo is used as a type.
> 
> Probably
> 
> typeid(typeof(foo))

Yes, but then you have:

---
void whatever (IFoo foo) {
   writefln(typeof(foo).stringof);
}

whatever(new Foo()); // prints IFoo
---

typeof is strictly compile-time. BC's looking for a runtime check.
November 22, 2007
Christopher Wright wrote:
> Robert Fraser wrote:
>> BC wrote:
>>> new question. since IInterface.classinfo returns IInterface, how to get
>>> the actual value of the concrete class?
>>>
>>
>> typeid.
>>
>> interface Foo { }
>> class Bar : Foo { }
>> // ...
>> Foo foo = new Bar();
>> TypeInfo ti = typeid(foo);
> 
> Error: foo is used as a type.

Oops, shouldn't shoot my mouth off unless I actually know what I'm talking about.

Attempt 2: A series of casts and .classinfo should work.