Thread overview
dummy question : runtime type testing...
Jun 18, 2011
Lloyd Dupont
Jun 18, 2011
Jonathan M Davis
Jun 18, 2011
Lloyd Dupont
Jun 18, 2011
Jonathan M Davis
Jun 18, 2011
Mike Wey
Jun 18, 2011
Lloyd Dupont
Jun 18, 2011
Lloyd Dupont
June 18, 2011
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 */ } 

June 18, 2011
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
June 18, 2011
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 

June 18, 2011
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
June 18, 2011
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
June 18, 2011
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
June 18, 2011
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