I want to get the declared type my number, but I have no idea how to get them. So I wrote a test with possible detection methods:
logNumber(Number(3.5), "Number");
logNumber(Number(3), "Number");
logNumber(Number(-3), "Number");
logNumber(uint(3), "uint")
logNumber(int(3), "int")
logNumber(int(-3), "int")
function logNumber(value:*, expected:String):void
{
trace("\n\n\n ");
trace("** Input value: " + value + "\n** Expected: " + expected + "\n")
trace("getQualifiedClassName: ", getQualifiedClassName(value) + check(getQualifiedClassName(value), expected));
switch (value)
{
case value as uint:
{
trace('as: uint' + check('uint', expected));
break;
}
case value as int:
{
trace('as: int' + check('int', expected));
break;
}
case value as Number:
{
trace('as: Number' + check('Number', expected));
break;
}
}
if(value is uint) trace("is: uint" + check('uint', expected));
else if(value is int) trace("is: int" + check('int', expected));
else if(value is Number) trace("is: Number" + check('Number', expected));
trace("describeType name:" + describeType(value).@name + check(describeType(value).@name, expected));
trace("typeof: ", typeof(value) + check(typeof(value), expected));
trace("\n" + describeType(value))
}
function check(type:String, expectedType:String):String
{
return "\n » " + (type == expectedType ? "good" : (type.toLowerCase() == expectedType.toLowerCase() ? "almost good" : "wrong"))
}
This outputs the following results to my trace panel:
3.5 as Number
** Input value: 3.5
** Expected: Number
getQualifiedClassName: Number
» good
as: Number
» good
is: Number
» good
describeType name:Number
» good
typeof: number
» almost good
<type name="Number" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
3 as Number
** Input value: 3
** Expected: Number
getQualifiedClassName: int
» wrong
as: uint
» wrong
is: uint
» wrong
describeType name:int
» wrong
typeof: number
» almost good
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
-3 as Number
** Input value: -3
** Expected: Number
getQualifiedClassName: int
» wrong
as: int
» wrong
is: int
» wrong
describeType name:int
» wrong
typeof: number
» almost good
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
3 as uint
** Input value: 3
** Expected: uint
getQualifiedClassName: int
» wrong
as: uint
» good
is: uint
» good
describeType name:int
» wrong
typeof: number
» wrong
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
3 as int
** Input value: 3
** Expected: int
getQualifiedClassName: int
» good
as: uint
» wrong
is: uint
» wrong
describeType name:int
» good
typeof: number
» wrong
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
-3 as int
** Input value: -3
** Expected: int
getQualifiedClassName: int
» good
as: int
» good
is: int
» good
describeType name:int
» good
typeof: number
» wrong
<type name="int" base="Object" isDynamic="false" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="*" optional="true"/>
</constructor>
</type>
It’s a lot to analyze, but some things are popping out:
- If a Number equals 3.5, it will be outputted as Number, but if it is 3 it will be outputted as int/uint.
- Its hard to detect if it is a uint or int, results are confusing and not consistent.
For a debug purposes (and just to understand how this works), I want the type that I have declared. How can I make a function that returns the right type?
There seems to be a reliable solution to find the right type of the number.
However there are some restrictions; It’s only possible for public vars, and you have to pass the parent object. If you use
describeTypeof the parent object gives the exact type of the Class properties, not how it is optimized at runtime. You have to find the property inside the object to find the right type of it.If you try this out on a simple MovieClip:
These values seems to be (always) right.
(Note: This function will be added to the new Reflection module of the internal version of the Temple library and probably will be available in the next release. This class nicely caches describeType too, for optimizing the describeType call)