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.