Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 30, 2010 dynamic type casting in D | ||||
---|---|---|---|---|
| ||||
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. |
May 30, 2010 Re: dynamic type casting in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to dave | dave: > 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++? D conflated all kinds of casts into the cast() syntax. So dynamic_cast can be done with a cast(). > I can't seem to find a way to check if a variable has a particular class or interface it inherits, would be nice. Cast it and if it is not null, then the dynamic cast is allowed. Or do you need some other information? Bye, bearophile |
May 30, 2010 Re: dynamic type casting in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to dave | 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.
|
May 30, 2010 Re: dynamic type casting in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert Clipsham | Awesome! I just tried it out, the cast expression is exactly what I was looking for. I'm currently using Tango and tried out its runtime traits api, this works as expected: if(implements(typeof(something).classinfo, A.classinfo)) Thanks for the info! |
May 30, 2010 Re: dynamic type casting in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to dave | On Sun, 30 May 2010 19:40:27 +0000, dave wrote: > if(implements(typeof(something).classinfo, A.classinfo)) in simpler way: 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 } or you can also create variable of type B inline: if (auto b = cast(B) o) { use b ... } you can find more type information using 'is' expression http://www.digitalmars.com/d/1.0/expression.html#IsExpression |
Copyright © 1999-2021 by the D Language Foundation