testing if an instance is a subclass of a given class

Paul Dufresne dufresnep at gmail.com
Tue Sep 16 21:34:29 PDT 2008


Well, I have decided that translating Python code to D would be a nice way to learn D.
It clearly shows me how D is a statically-typed language, and Python a dynamically-typed language.

I'd like to translate to D:
  if issubclass(type(item), Base):
    item.on_value_changed(True)
  elif issubclass(type(item), Group):
    item.on_last_change()

My problem is to translate issubclass.

using typeid(item) I would get some info about the type... but it is unclear how to test for subclass from there.

item is(item:Base) is a compile-time test, so it does not seems to do what I want.

I am thinking about something like:
if (cast (Base) item)!=null then
     item.on_value_changed(True);
else if (cast(Group) item)!=null then
        item.on_last_change();

oh... as I am writing I see the suggestion in spec_DMD_1.00.pdf to do:
In order to determine if an object o is an instance of a class B use a cast:
if (cast(B) o)
{
     // o is an instance of B
}
else
{
     // o is not an instance of B
}

My guess is that it is really a subclassing test... is it?

And would it be possible to write an isSubclass function in D based on this test...
I don't see how I would pass the Class for the test... what come in mind naturally would be:
bool isSubclass (Class C, Object o){
  if (cast (C o) then return true else return false;
}

but the Class type does probably not exist ... does it?


More information about the Digitalmars-d-learn mailing list