how can i use the derive-hierarchy at runtime for typechecking?
Regan Heath
regan at netmail.co.nz
Fri Sep 21 01:49:08 PDT 2007
dennis luehring wrote:
> 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
Because using 'auto' like this only works in an "if" statement. eg.
if ( auto check = cast(typeof(this))other ) return true;
return false;
> // 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
writefln(typeof(this).stringof) shows "base" even when called on an
object of type "test" or "blub". So, even if you say:
test t = new test;
blub b = new blub;
if (t.is_derived_from(b)) {} //is 'test' derived from 'blub'
you're really just asking if t is derived from "base", which it is (has
to be to have the is_derived_from method - in this case)
Regan
More information about the Digitalmars-d-learn
mailing list