MXMLC WTF (3)

This isn’t strictly a MXMLC WTF, but rather an AS3 WTF, but I’ll stick it here anyway. Look at the following line:

var list : Array = new Array();

This line contains at least three WTF’s. Read on…

  1. I have to include the type in the variable declaration even though the compiler simply can infer which type the variable has.
  2. I have to tell the compiler that this is a variable, which is also simple for the compiler to infer from the context.
  3. Even though the variable is statically typed, it doesn’t tell me nor the compiler very much, since the type of the contents of the array are unknown.

These deserve some further explanations:

  1. This is just plain stupid. Type inference is not rocket science. MTASC could compile AS2 with type inference, and it worked like a charm. There is no reason why local variables should ever have to be statically typed (instance variables and method parameters should, however, always be since it’s part of the informal documentation of the code). Statically typed local variables doesn’t make the code more readable, on the contrary, ActionScript’s syntax makes typed code less readable if anything.
  2. Fair enough, without the var the compiler would have to search the global and class scope first to see if the variable was already defined, and it wouldn’t know in which scope to define it if it wasn’t. However, having to type both var and Array feels over the top, one should be enough, and is enough in all other languages with static typing.
  3. This one only applies to collection types, and for arrays there is a compiler directive to make the compiler type-check them ([ArrayElementType("Number")]). It can’t be used to declare the return type of functions, however, which makes it only half-usable.

Conclusion

Whoever defined the AS3/ECMAScript specification must have felt that backwards compatibility was more important than readability, and the Adobe engineers doesn’t understand type inference (I doubt that the spec requires static typing, since it tries to be backwards compatible).

In AS3 the var is redundant, as is the function keyword (except for non-method functions and function literals). I’ve said it before, Adobe should look more at C# and not continue the folly of trying to make a language of their own, because they are not very good at it.

Long live :*

9 Responses to “MXMLC WTF (3)”

  1. Paulius Uza Says:

    You should have a read on actionscript 3 performance tuning using strong typing – it explains a lot http://www.onflex.org/ted/2006/09/max-actionscript-3-performance-tuning.php

  2. JesterXL Says:

    I disagree with everything except your last line:

    Long live :*

    • is the bomb!
  3. ashif sayani Says:

    You can still do this:

            var list = ["hello","world"] 
            list.push(",ashif")
            Alert.show( list[2].toString())
    

    AS3 can stil be used as a weakly-typed, dynamic language. Adobe just doesn’t recommend using it that way for performance reasons. They say the VM runs up to 10x faster with strongly typed, static code.

  4. Theo Says:

    I must admit that I didn’t know about the strong typing compiler optimizations. If I had, I would have chosen my words differently as to avoid the confusion. The optimizations are, however, not relevant in this context as they apply to typing instance variables and method parameters, and not local variables, which is what I’m talking about here.

    It’s not typing as such that I’m against, it’s how the compiler forces me to type code which doesn’t need typing. The compiler can do all those optimizations without me having to explicitly type local variables. Check out type inference on Wikipedia: http://en.wikipedia.org/wiki/Type_inference

    ashif: “Warning: var ‘list’ has no type declaration.” Your code compiles, with a warning, and it’s that warning that I’m against.

  5. ashif sayani Says:

    I 100% agree with you that it sucks have to type code that doesn’t need it. After using Ruby for the last little while (coming from a C# background) and enjoying the productivity a weakly-typed language gives me, I feel the same as you do, that Adobe is going the wrong way on this.

  6. julien Says:

    Hi,

    Interesting post … but if you dislike ActionScript so much, why are you using it?

  7. Theo Says:

    I dislike many aspects of ActionScript and of the Flash API:s, but the fact is that the Flash platform is the best by far if you want to make RIA:s and there is no alternative.

  8. Veli Ogla Sungutay Says:

    Good work with these WTFs. :) I can’t say anything about the “var”, but you should definitely check out the haxe language and compiler, at http://haxe.org

  9. Stu Says:

    “Long live :*” ??

    Man, I’m glad I don’t have to pick up your code !

Leave a Reply