how can i use the derive-hierarchy at runtime for typechecking?

dennis luehring dl.soluz at gmx.net
Thu Sep 20 23:42:13 PDT 2007


Jarrett Billingsley schrieb:
> "Chris Nicholson-Sauls" <ibisbasenji at gmail.com> wrote in message 
> news:fcupko$kh7$2 at digitalmars.com...
> 
>> void ifIs (S, T) (S obj, void delegate(T) dg) {
>>   if (auto casted = cast(T) obj) dg(casted);
>> }
> 
> Or you could skip Downs' confusing way of writing code entirely ( ;) ) and 
> just use:
> 
> if(auto b = cast(B)foo)
> {
>     // do stuff with b
> } 
> 
> 

and how can i insert this test in my base-class to hide the type stuff

class base
{
   int base_bla;

   bool is_derived_from( base other )
   {
     auto check = cast(typeof(this))other;
     return !( check is null );

     // why can't i compile this
     // return ( auto check = cast(typeof(this))other );
   }
}

class test: base
{
   int test_bla;
}

class blub: test
{
   int blub_bla;
}

the is_derived_from test is always true
maybe because of the base type parameter in is_derived_from

and the other thing i need to know(show) the derive-hierachy

"the hole story"

in my c++ project i use objects which represents the type of an object
for example

class car // real implementation
{
}

class car_class // object-type of class car
{
   car create_instance(); ...
   bool is_derived_from
   base_class[] derived_from_me();
   base_class[] base_class_of();
}

and many [whatever]_class objects in my known-types-pool

for example:
i can ask which "types" i know
i can create real types of it
i can ask for the derives/bases of an "type"
i can test if an "type" is hierachical "compatible"=derived from with 
another "type"

and just need to know my base-class interface for doing this
...

all this is done by using my own decription.language (with derive 
ability) and an c++ code generator...

i hope to find a better (more compiletime based) way to get the same 
features - i want to get rid of this generator stuff

ciao dennis


More information about the Digitalmars-d-learn mailing list