rtti cast

BCS BCS at pathlink.com
Thu May 1 15:20:12 PDT 2008


Simon Buerger wrote:
> hi
> I would like to do some RTTI, but not with type-IDs, but in some more 
> pretty way. I'll simply post some code to show you what I mean :) . My 
> Question is now: Does anyone know, how to do sth alike with the current 
> D implementation (maybe with templates, but I dont see a nice way) ? Or 
> do you think it could be worth an extension for D 2.0 ?
> Krox
> 
> Code:
> 
> class Base {}
> class A : Base
> {
>     void function_A() { ... }
> }
> class B : Base {}
> {
>     void function_B() { ... }
> }
> 
> void main()
> {
>     Base x = ...
>     
>     // checks if x is of type A, and if so, declares y as A alias of x 
> with other type
>     if (x == A y)   
>         y.function_A();
>     else if(x == B y)
>         y.function_B();
> }

this work?

if (auto y = cast(A)x)
	y.function_A();
else if(auto y = cast(B)x)
	y.function_B();




More information about the Digitalmars-d mailing list