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…
- I have to include the type in the variable declaration even though the compiler simply can infer which type the variable has.
- I have to tell the compiler that this is a variable, which is also simple for the compiler to infer from the context.
- 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:
- 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.
- Fair enough, without the
varthe 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
Arrayfeels over the top, one should be enough, and is enough in all other languages with static typing.
- 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.
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.