dynamic type casting in D

Robert Clipsham robert at octarineparrot.com
Sun May 30 12:05:30 PDT 2010


On 30/05/10 19:46, dave wrote:
> Hi,
>
> sort of new to D programming, coming from C++. Basically the question is if
> there is some sort of a way to dynamically cast a variable in D much like you
> do in C++?
>
> ex:
>
> interface A {...}
> class B {...}
> class C : B {...}
> class D : B, A {...}
>
> function(B something) {
>      A a = dynamic_cast<A>(something);
> }
>
> I can't seem to find a way to check if a variable has a particular class or
> interface it inherits, would be nice.

void func(B something)
{
	A a = cast(A)something;
}

Is what you're looking for I believe. As for your other request, take a 
look at std.traits, __traits, or if you're using D1 std.traits, 
tango.core.Traits, tango.core.RuntimeTraits.


More information about the Digitalmars-d-learn mailing list