Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
August 31, 2014 How to compare two types? | ||||
---|---|---|---|---|
| ||||
How to compare two types? Will I use T.stringof instead of this? void main() { if(One is Two) {} //Error: type One is not an expression //Error: type Two is not an expression } class One {} class Two {} Regards, MarisaLovesUsAll |
August 31, 2014 Re: How to compare two types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MarisaLovesUsAll | There's two ways: static if(is(One == Two)) { } That compares the static types in a form of conditional compilation. http://dlang.org/expression.html#IsExpression If you want to compare the runtime type of a class object, you can do: if(typeid(obj_one) == typeid(obj_two)) that should tell you if they are the same dynamic class type. |
August 31, 2014 Re: How to compare two types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | Adam D. Ruppe:
> If you want to compare the runtime type of a class object, you can do:
>
> if(typeid(obj_one) == typeid(obj_two))
>
> that should tell you if they are the same dynamic class type.
And what about:
if (is(typeof(obj_one) == typeof(obj_two)))
Bye,
bearophile
|
August 31, 2014 Re: How to compare two types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sunday, 31 August 2014 at 23:53:31 UTC, bearophile wrote:
> if (is(typeof(obj_one) == typeof(obj_two)))
You could, but since it is static info you might as well use static if.
|
August 31, 2014 Re: How to compare two types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | typeof() always gets the static type, typeid() is needed if you want the dynamic type. |
September 01, 2014 Re: How to compare two types? | ||||
---|---|---|---|---|
| ||||
Posted in reply to MarisaLovesUsAll | Thanks for the answers! |
Copyright © 1999-2021 by the D Language Foundation