Discover ECMAScript 4: The Future of ActionScript

Update: Since I wrote this article, some interesting news has surfaced. New information about this topic is available in my articles Some controversy over ECMAScript 4 and How will ECMAScript “Harmony” affect ActionScript 3.

The ECMAScript working group recently released a (PDF) work-in-progress overview of ECMAScript 4. What is ECMAScript? According to the official website, the working group calls it “the language of the web”. More specifically, it’s the basis for languages such as JavaScript and Flash’s own ActionScript.

The official release of ES4 won’t happen until Fall 2008, but the working group releases updates from time to time to keep us informed (or salivating, depending on your level of geekiness). Adobe, a member of the working group, used an earlier draft of ES4 as the basis for AS3, and they’re “publically committed to sticking to [the ES4] standard” (source). The current update to ES4 includes many changes that we will probably see in a future revision to Flash’s programming language. I read the entire document from beginning to end, and I wanted to share some of my favorite discoveries.

Core Classes

Object and Array have picked up some new friends. In particular, a class called Map works a lot like Object, but it allows you to use any value as a key (not just a String). This is very similar to flash.utils.Dictionary in AS3, but there’s one interesting addition that I’ll get to in a moment. Similar to Array, there’s a new class named Vector. Of note, it provides bounds checking, and you can optionally specify a fixed length. Using a new language feature, both Map and Vector are parameterized types. Vector is actually what some might consider a typed Array. When you create an instance, you must specify the datatype of the objects you intend to place inside the Vector. Similarly, to use Map, you must specify both the key datatype and the value datatype. An example borrowed from the PDF overview should help illustrate how these will work:

//create a Map with keys of type * (known as any type) and values of type int
var example1:Map = new Map.;

//create a Vector to contain integers
var example2:Vector = new Vector;

Language Features

Some of the coolest additions to ES4, in my opinion, are record types and the like keyword. Think of record types as ultra-light classes. They define members, but aren’t an entire class definition. Here’s an example of a record type:

type Point = { x:Number, y:Number };

It should be no surprise that you can create new instances of these types (you’ll get a regular Object, though) like any class.

var myPoint:Object = new Point();

What’s more interesting is that you can use the like keyword with record types to produce some interesting results. In AS3, a MovieClip has x and y properties that are both instances of class Number. This makes a MovieClip like the Point type described above (don’t confuse it with flash.geom.Point).

var clip:MovieClip = new MovieClip();
clip like Point //true

Imagine being able to run a function on any object that has x and y properties without requiring a special interface or setting a parameter’s type to Object (where you’d have to do runtime checks to ensure that x and y even exist!).

Another useful class-like feature is something called a union type. To understand union types, think of a current feature of AS3: Number, uint, and int are all pretty much interchangeable. That’s because ES4 defines an internal union type AnyNumber to accept and convert all numeric values as needed (notice a few new numeric types, by the way).

type AnyNumber = (byte,int,uint,double,decimal,Number);

The new let keyword gives us a more familiar block scoping mode used by other languages. In the current incarnation of AS3, a variable in a function is always accessible from anywhere in that function, by using let instead of var, you can make a variable that is only in the scope of a for-loop or an if statement. Excellent.

A feature I see requested often is operator overloading. Thankfully, this will be made possible thanks to another new feature generic functions. These functions are outside of classes and they’re pretty close to the bare metal of the runtime. Here’s an example:

generic intrinsic function + ( a:String, b:MyCustomString )
{
    //some operation that concats a MyCustomString with a regular String
}

The docs say that you cannot override a generic operator that has been implemented already, but you can add new ones that support your own custom classes. I’m sure some folks might complain, but I’d say it’s good enough for me. I’d hate to see the messes some dumb developer could create if he changed the way the core types behave with operators.

Finally, I’d like to share iterators. Two new types, IterableType and IteratorType give you the ability to code your classes to work with for-in and for-each-in loops more easily. Previously, in AS3, developers could only iterate over dynamic properties with these loop types. Now, by implementing a couple methods, get() and next() you can give a little more power to your applications, but only if needed. Older AVM1-era Flash applications could always iterate over properties, but today, that’s been removed for a performance boost. Nice to see this feature come back, if a developer chooses to use it.

That certainly isn’t everything new that we’ll see coming in the final version of ES4. I just picked out a few particular things that caught my eye and seemed the coolest. Be sure to read the ECMAScript 4 Overview PDF for full details.

Just as I was “impatiently waiting for ActionScript 3” back in the alpha days, you can bet that I’m just as excited for the next version today. Too bad it could be years away! When? Who knows. I’d say I’m pretty safe guessing that Flash Player 10 is out of the question. Adobe has their hands full with the features they’ve shared already. Furthermore, they would be silly to introduce major language revisions in two consecutive player versions. I’m also still seeing many learning the migration path from AS2, and I don’t think that dangerous sea will calm for a while. Definitely FP11 at the earliest, and after the ES4 spec has been finalized. In other words, be patient, but smile and dream of the cool coding tricks you’ll be able to do someday.

About Josh Tynjala

Josh Tynjala is a frontend developer, open source contributor, bowler hat enthusiast, and karaoke addict. You might be familiar with his project, Feathers UI, an open source user interface library for Starling Framework that is included in the Adobe Gaming SDK.

Discussion

  1. Ian

    Hi Josh, Great post – with the addition of ‘let’ do you know if it will extend to variables in for loop definitions? E.g;

    for( let counter : int =0; counter < X < counter++ )
    {
        blah;
    }
    
    for( let counter : int =0; counter < Y < counter++ )
    {
        blah;
    }

    At the moment the compiler warns you that you have redefined ‘counter’ – will this be different with as4?

  2. pan69

    We will see AS4 when we’ve bought all the books on and done the courses on AS3 etc etc so we can buy it all over again… 🙂

  3. Pingback: Janek (Can I) KÅ‚osiÅ„ski » ECMAScript 4

  4. Tink

    Also looks like it will feature typed arrays which is cool and hopefully will reduce the amount of casting we have to do with array elements.

  5. Josh Tynjala

    Ian, yes. The counter in a for loop was actually a use-case example for the let keyword in the document.

    Tink, as I stated, Vector is the new typed Array. The document says that it will have all the methods of Array, and it will be a parameterized type so that you can specify the type of objects it contains.

  6. Josh Tynjala

    ykr, thank you for sharing such highly intelligent and constructive criticism. It is clear that you must be exceptionally enlightened. Truly, you’ve given us a comment full of wonderful and profound insights, and your contribution to the conversation is absolutely remarkable.

  7. Tom Ortega

    Sweet post, Josh. Thanks for reading and dissecting for us. I’ll have to download the PDF and put it in the “To Read” pile. If you feel like doing a follow up “Interesting things, Part II” I’m sure we’d appreciate it. I could then move it to the “Read at some point” pile. 🙂

  8. Pingback: Future of ActionScript (via ECMAScript) and language talk « Tom’s Blog

  9. Josh Tynjala

    If you feel like doing a follow up “Interesting things, Part II” I’m sure we’d appreciate it. I could then move it to the “Read at some point” pile.

    So what you’re saying, Tom, is that you’re lazy. Haha. Glad you liked my juicy summary 🙂

  10. Josh Tynjala

    Interesting, Lordy. It’s certainly possible that they will implement Vectors for Flash Player 10 if they’re going to have “typed Arrays”. However, I imagine they could implement it in a different way through the flash.* package if Vectors aren’t considered set in stone in the ECMAScript spec. We’ll just have to wait and see.

  11. Pingback: judahs blog » Blog Archive » Design and Development Convergence

  12. Pingback: Some Controversy over ECMAScript 4 - Zeus Labs

  13. Simoens

    No, I realise that, I was just wondering if anyone knew about it being possible in te future, sorry if I went off-topic

  14. Josh Tynjala

    I’m not aware of any plans to add any form of function overloading (except operators) in ECMAScript 4. Certainly, they could add it in future versions, though.

  15. Pingback: El futuro de ActionScript » Joan | Garnet Flex:Flash:PHP:MySQL:JS

  16. Pingback: Live From 360Flex, Day One - Zeus Labs

  17. John

    Nice article, good to know what to expect. YKR… I can understand why you can’t learn newer versions of AS I mean you can’t even spell RULES….After just reading Colin Moock’s 1000 page book on actionscipt 3 I am excited about 4 but I’m fine waiting a bit and adjusting more to 3 before the next transition.