www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dummy question : runtime type testing...

reply "Lloyd Dupont" <ld-REMOVE galador.net> writes:
given
----
A a = ....
class A {}
class B : A {}
----
how could I test, at runtime, if "a" is just a "A" or is a "B"?


also, give a type T and a TypeInfo variable, how can I know if a variable of 
type T is attributable to a variable of type described by TypeInfo?
i.e. how could I implement the method below?
bool isTypeOf(T)(TypeInfo ti) {  /* how to check and return whether T is a 
ti or a subclass */ } 
Jun 17 2011
next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-06-17 19:58, Lloyd Dupont wrote:
 given
 ----
 A a = ....
 class A {}
 class B : A {}
 ----
 how could I test, at runtime, if "a" is just a "A" or is a "B"?

Cast it. The result will be null if the cast fails. if(cast(B)a) //it's a B else //it's an A but not a B - Jonathan M Davis
Jun 17 2011
parent reply "Lloyd Dupont" <ld-REMOVE galador.net> writes:
ho... easy hey!
but how about the other 2 test I'd like be able to run?

bool isTypeOf(T)(TypeInfo ti) {  "return T is ti" }
bool isTypeOf(TypeInfo ti1, TypeInfo ti2) {  "return ti1 is ti2" }



"Jonathan M Davis"  wrote in message 
news:mailman.988.1308367581.14074.digitalmars-d-learn puremagic.com...

On 2011-06-17 19:58, Lloyd Dupont wrote:
 given
 ----
 A a = ....
 class A {}
 class B : A {}
 ----
 how could I test, at runtime, if "a" is just a "A" or is a "B"?

Cast it. The result will be null if the cast fails. if(cast(B)a) //it's a B else //it's an A but not a B - Jonathan M Davis
Jun 17 2011
parent reply Mike Wey <mike-wey example.com> writes:
On 06/18/2011 05:28 AM, Lloyd Dupont wrote:
 ho... easy hey!
 but how about the other 2 test I'd like be able to run?

 bool isTypeOf(T)(TypeInfo ti) { "return T is ti" }
 bool isTypeOf(TypeInfo ti1, TypeInfo ti2) { "return ti1 is ti2" }

bool isTypeOf(T)(TypeInfo ti) { return ( typeid(T) is ti ); } bool isTypeOf(TypeInfo ti1, TypeInfo ti2) { return ( ti1 is ti2 ); } -- Mike Wey
Jun 18 2011
next sibling parent "Lloyd Dupont" <ld-REMOVE galador.net> writes:
That simple hey?!?
Awesome!
You are a week-end saver! :)

"Mike Wey"  wrote in message news:ithv61$2j5t$1 digitalmars.com... 

bool isTypeOf(T)(TypeInfo ti)
{
     return ( typeid(T) is ti );
}

bool isTypeOf(TypeInfo ti1, TypeInfo ti2)
{
     return ( ti1 is ti2 );
}

-- 
Mike Wey
Jun 18 2011
prev sibling parent "Lloyd Dupont" <ld-REMOVE galador.net> writes:
on a related not.
in the (templated) function taking a variant, I use the coerce() method 
which does some conversion (byte to int, for example)

the suggested code (while excellent, much thanks again!) is very stringent, 
doesn't allow for such implicit conversion

i.e. with
bool isTypeOf(TypeInfo ti1, TypeInfo ti2) { return ( ti1 is ti2 ); }

isTypeOf(typeid(byte), typeid(int)) will return false.

is there (I might be pushing it here, I know :P) a way to make it return 
true in such case??





"Mike Wey"  wrote in message news:ithv61$2j5t$1 digitalmars.com...

On 06/18/2011 05:28 AM, Lloyd Dupont wrote:
 ho... easy hey!
 but how about the other 2 test I'd like be able to run?

 bool isTypeOf(T)(TypeInfo ti) { "return T is ti" }
 bool isTypeOf(TypeInfo ti1, TypeInfo ti2) { "return ti1 is ti2" }

bool isTypeOf(T)(TypeInfo ti) { return ( typeid(T) is ti ); } bool isTypeOf(TypeInfo ti1, TypeInfo ti2) { return ( ti1 is ti2 ); } -- Mike Wey
Jun 18 2011
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-06-17 20:28, Lloyd Dupont wrote:
 ho... easy hey!
 but how about the other 2 test I'd like be able to run?
 
 bool isTypeOf(T)(TypeInfo ti) {  "return T is ti" }
 bool isTypeOf(TypeInfo ti1, TypeInfo ti2) {  "return ti1 is ti2" }

I'm afraid that I don't know much about TypeInfo. In my experience, compile- time reflection is more than enough. So, I've never attempted any kind of runtime reflection in D, and I've never found any use for TypeInfo. So, I'm not particular well-informed on what information TypeInfo has in it. - Jonathan M Davis
Jun 17 2011