I saw this post about Numbers, ints, and uints:
https://troyworks.com/blog/?p=67
and started thinking up a nice answer but then saw that the blog does not accept comments. So I’ll post my comments here. The basic point was creating a variable as a Number, assigning it an integer or unsigned integer value (and vice versa) and then tracing out what type it held.
The following code was used:
[as]var a:int = 1;
var b:uint = 2;
var c:Number = 3;
var d:Number = 4.5;
var args:Array = [ a, b, c, d];
function testNumber(a:Object):void{
trace(a + †——â€);
trace(†isNumber?†+ (a is Number));
trace(†isInt?†+ (a is int));
trace(†isUint?†+ (a is uint));
trace(†typeof number? †+ (typeof(a) == “numberâ€));
}
for(var i in args){
testNumber(args[i]);
}[/as]
The author was surprised, I think, that in cases 1, 2, and 3, the result showed that the value was both a number and an int / uint.
There are two factors here. One is inheritance. Although the documentation says that int, uint, and Number all inherit from Object, it’s obvious that there is some relationship between the three. Otherwise the following code would not compile:
[as]var a:int = 1;
var b:Number;
b = a;[/as]
I think it is safe to imagine an inheritance structure between these three, where Number is the most general type, int is more specific, and uint is more specific still. Thus an uint “is a” int, which “is a” Number, in the same way that a MovieClip “is a” Sprite, because it extends it:
[as]var m:MovieClip = new MovieClip();
trace(m is Sprite); // true[/as]
That accounts for why variables typed as ints/uints and assigned ints/uints trace true for being Numbers.
But what about the third case, where a Number variable is assigned the value 3, and then traces out true for int and uint? Well here we have the difference between a variable and a value. A variable is a container. You can give it a type, which tells you what type of objects that container can hold. That doesn’t tell you the actual type of the object it is holding though. It may be holding a related, more specific type. Altering the last example a bit:
[as]var m:Sprite = new MovieClip();
trace(m is MovieClip); // true[/as]
The variable m is assigned the type Sprite. But it is actually holding a MovieClip. The variable has a type, but the trace occurs a runtime and looks at the actual object and its type, not the container’s type.
In the author’s example, the value 3, is an int, and a uint. The variable is typed as Number, but at runtime, the trace method looks at the object in there, which is indeed an int/uint, and reports what it finds.