views:

316

answers:

2

How do I check the type of a value on runtime?

I'd like to find out where I'm creating doubles.

A: 

With very few exceptions, you never need to check type at runtime. Typed variables can only hold their assigned types, and type promotion is determined at compile time.

Sparr
I think people have marked you down because you've brushed over the situation where the type is lost due to passing/copying in a void pointer (where type is lost completely). In this case, original type information is unrecoverable.
Matt Gallagher
Yes, it is unrecoverable, and thus not a situation covered by any possible answer to this question anyways.
Sparr
+3  A: 

If you're using Objective-C classes, then the [myObject isKindOfClass: [InterestingClass class]] test is available. If you're using primitive types (which your question, quoting the "double" type, suggests), then you can't. However unless you're doing some very funky stuff, the compiler can tell you when primitive types do or don't match up, and when it doesn't will perform implicit promotion to the desired type.

It would be beneficial to know a little more about what the specific problem is that you're trying to solve, because it may be that the solution doesn't involve detecting the creation of doubles at all :-).

Graham Lee
Hey Graham. I read that doubles in the iphone should be avoided. I'm using floats with quite a lot of digits in some places, so I just wanted to check if those were actually doubles.
Steph Thirion