The confusion here stems from a peculiar subtlety about how AS3 handles types and numbers. The is
operator tests class inheritance, but int
and uint
are not actually classes. (That's why they don't get capitalized - because they have no class definition.) They're more like type associations, which if used properly can gain you certain conveniences and performance improvements. But for inheritance purposes, a Number is a Number is a Number.
What this means in practice is, if you make a variable that is typed as, say, uint
then Flash will store that value internally in a 32-bit unsigned format (rather than the 64bit format used for Number
). And if you change the value of that variable, it will remain in the same format, so the restrictions on uint
will be enforced:
var a:uint = 0;
a--;
trace(a); // 4294967295 - it wrapped around
But it's really the reference to your number that is typed as uint
, not the number itself. If you make a new untyped reference, this will be apparent:
var a:uint = 0;
var b:* = a;
b--
trace(b); // -1
So to return to your problem, how should you implement your buffer writer? Due to the inherent subtlety in how Flash treats these types I don't think there's an absolutely correct answer. One approach would be to use uint
or int
if the data meets the restrictions on those types, and use Number
otherwise. This would save memory and it could preserve accuracy. But treating all numbers as Number
also strikes me as a defensible approach. I think it depends on what you plan to do with the buffer.