views:

139

answers:

3

According to the ActionScript 3.0 documentation, the TextFormat classes size property is an Object. Why is this? Clearly this is a number! When I check its type (using typeOf()), it returns Number. So whats the deal?

+1  A: 

No clue, just some ideas... quite a lot of properties are actually typed as Objects when there's a possibility to pass various types (e.g String in this case). This just makes it easier since you don't have to cast the type and convert it before applying the value. Also, note the initial value is null(not NaN as for a Number) . If text formats size is read when the value is null the getter will return the default size. This way you can reset the value to default by simply setting it to null.

Theo.T
the only type that can hold the value of `undefined` is `*` all others are `null` except `int` (`0`), `uint` (`0`), `Boolean` (`false`) and `Number` (`NaN`)
back2dos
Very true, thanks for spotting the typo. I'm correcting this.
Theo.T
+1  A: 

Every object you use in ActionScript inherits from Object, which is why you can create an object with var newObject:Object and assign it to be any type of existing object of a different type. Look in the docs at Number; it says at the top that its inheritance is from Object. It even says this for int, which is really considered a primitive.

I guess having that particular property return Object just abstracts the return type somewhat, not locking it in to an object further down the inheritance chain. If for some reason it has no number to return it can return null instead of undefined, like Theo.T said.

You don't really need to worry too much about it anyway, as you'll always get a Number out of it :)

debu
A: 

The reason is that several primitive types are not nullable, including Number (also Boolean, int and uint). However null` means that the property has explicitely no value and shouldn't affect the according text property when applied to a text field. while in theory one could maybe use values as -1 or NaN, this is clearer.

I currently see it as my job to lure AS3 developers to switching to haXe :) Thus, on a slightly unrelated matter, I'd like to point out that this is one of many weaknesses in AS3's type system. haXe defines the type of such value as Null<Float>. If you do like strict typing, then you definitely should check out haXe.

greetz

back2dos

back2dos
-1 for "I currently see it as my job to lure AS3 developers to switching to haXe"
Sam
@Sam: care to explain?
back2dos
@back2dos, I don't think recommending an alternate language is appropriate in this context. It's my opinion, but since someone else also voted down this answer, at least one other person shares it apparently.
Sam
@Sam: I came to realize that about 25% of the AS3 questions I read are simply moot in haXe. that and my personal experience lead me to recommend it to others, especially when haXe solves the problem they have, which in this case is a semantically unclear API. Also, I don't see why this is unappropriate, because this question obviously is about the flash player API and AVM2 and much less about AS3 itself. Also, I'd like to point out, that this answer has a first paragraph, which is correct and answers the question. Anyway, thanks for your opinion, altough I can't understand it at all.
back2dos
@back2dos, I see the first paragraph, which is good and correct and would have garnered an up-vote had it not been for the second paragraph.
Sam