Flex: getType() schmettype

I’ve started dabbling in Flex recently, and something that was apparent(and annoying at times) to me  early on was it’s strongly-typed nature. Not type as in a keyboard, but variable types. Flex cares very much whether your variable is an Array, or ArrayCollection as it’s knowledge of types is key to it’s processing. This is quite the converse to a language like PHP, which is loosely-typed to say the least.

Anyway, I came upon a case where I needed to know the type a variable was so I could cast it properly(and for debugging), and I found that Flex doesn’t have a function to simply return a variable’s type. There is describeType(), but this returns an XML object with all of the information about the variable — not just the type. Plus you have to import the proper class to use it. So, I threw together this function quickly that will perform the import, and return/alert the type as needed:

private function getType(value:*, alert:Boolean = false):String
{
	import flash.utils.*;
	var description:XML = describeType(value);
	if(alert)
		Alert.show(description[0].@name);
	return description[0].@name;
}

I hope someone else can find it useful. 🙂

9 Comments


  1. Awesome, thanks!


  2. Glad it was helpful!


  3. Lawrie, thanks for bringing that up! getQualifiedClassName() does seem to be the same as the “name” node in describeType()’s return value.


  4. Why not just set your compiler to standard mode if you hate type checking so much?

    To me type checking:
    1. Moves many runtime errors to compile time
    2. Opens doors to polymorphism


  5. It’s not that I hate type checking, I just expected there to be an easier way to get just the type ala PHP’s gettype().