AS4 Feature proposal: Type inference

! Warning: this post hasn't been updated in over three years and so may contain out of date information.

AS4 draftThis is the first in a series of articles I plan on creating addressing important features that I feel are missing from ActionScript 3. They are features that I’d like to see added to a future version of ActionScript, which I’ve assumed would be called ActionScript 4 (AS4).

This first article is on type inference. So let’s kick straight off by considering the following lines of code:

In every case, the developer is subjected to the frustration of telling the compiler twice what type the variable should be. In some cases, the type is explicitly stated after the “=”; in other cases it is trivial for the compiler to work it out. The term for the compiler working it out is called “type inference”. As ActionScript uses the var keyword already and places the type after the variable name, the change in syntax to support type inference becomes trivial as shown below.

Certain expressions are inherently type-ambiguous. Where simple rules can be used to resolve that ambiguity, they should be employed. In other cases, the compiler should report an error. Consider the following examples:

Whilst the syntax is the same as AS2-style dynamic typing, the implementation is very different. Dynamic typing examines the type of the value being assigned at runtime in order to determine a temporary type for the variable. If a different type is assigned, the variable’s type changes. Type inference would only work with strict typing enabled and the variable’s type is fixed at compile time. It is simply inferred from the initial value, rather than being explicitly specified. Dynamic typing would still be supported through the use of the asterisk notation:

It should be possible for the compiler to infer types from complex expressions. The restriction being that the type of the expression must be unambiguous, or fall within the rules for resolving ambiguous expressions.

In the final example above, the type of variable g must be inferred from variable f. Variable f though is a dynamically typed variable and so has no type. One approach would be to make variable h dynamically typed too. However this then uses statically typed inference to create a dynamically typed variable, which is probably a bad idea. Therefore I’d propose the compiler generate an error instead.

3 thoughts on “AS4 Feature proposal: Type inference

  1. Interesting idea. As a slightly old skool programmer, inference scares me because I don’t trust compilers 😉

    However, if I look past my paranoia, I suspect this could actually strengthen the typing in an average AS program ’cause the compiler would start enforcing types after they had been first set.

    How do you see interfaces or base classes being dealt with? So things like:

    var a :IFoo = new MyThingWithFoo();
    var b :Sprite = new MySpecialIckleSprite();

  2. @Mnem,

    Interfaces and base classes are dealt with just as your code shows: explicitly. If your two lines instead read:

    var a = new MyThingWithFoo();
    var b = new MySpecialIckleSprite();

    then the compiler would infer that a should be of type MyThingWithFoo and b of type MySpecialIckleSprite. To override this inference in order to explicitly make the variables by of interface or base class types, then the variable’s type would still be explicitly specified using existing syntax.

Comments are closed.