ActionScript 3 Syntax preview
Update: Alas, I knew this would be short-lived in value when I posted it. =) Now that ActionScript 3 information has been released by Macromedia, you’ll find these links to ActionScript 3 language descriptions and differences from AS2 more useful than this preview based on the ECMAScript proposal.
I have to admit, since I first heard details about ActionScript 3, combined with the details of Flex Builder 2 (finally Flex the way I wish it was from v.1) I have been jumpy with excitement to tell people about what’s coming: coworkers, users’ group members, and even my wife has gotten an earful. I definitely consider this to be the best thing to happen yet in ActionScript development. (Maybe I should have applied for that Flex Technical Evangelist position, since that’s what I find myself wanting to do anyway!)
So, being unable to wait I dug up the ECMAScript (ECMA-262) 4th edition proposal and combed through it to find out what I could about what’s coming in ActionScript 3. Obviously Macromedia can choose what things to implement or not implement, but they have said (I’m paraphrasing here) that their goal is to be as close to the spec as they can, even the target implementation of the spec. So I think by looking at the spec we can get a pretty good idea at least from the syntax point of view what ActionScript 3 will be like. Of course, the object model is another story — for that we’ll just have to wait to see what Macromedia gives us, and what we as a developer community do with it!
So here’s my summary of the things in the ECMAScript 4th edition proposal which are not included in ActionScript 2. If you are a Macromedia employee, feel free to skip to the end to read other things that I would like to see implemented in addition to what’s in the spec.
Note: in addition to the things included here, Macromedia has publicly stated that AS3 will also support DOM Level 3 events and E4X, which are different specs (E4X is specifically an extension to ECMAScript).
Disclaimer: This is obviously just my interpretation of the ECMAScript spec; it is very possible that I missed or misinterpreted something, so this information may not be correct. In addition, Macromedia has the option of choosing to implement or not implement portions of the spec at their pleasure. Having said that, if you believe a correction is in order, please leave a comment or contact me.
Keywords
const- defines a constant. Must be a compile-time constant value; cannot be written to more than once.export- ???namespace- used to define a namespace (see below)package- used to define a package (see below)use- specifies a namespace to use (see below) or a pragma to use (whether to use strict mode or not is the only pragma defined in the spec)
Operators
as- “a as type” whereais a variable/value andbis a type. Returns the value ofaifacan be implicitly coerced totype, otherwise it returnsnull(or throws an exception iftypecan’t be null).is- “a is b” returnstrueifais a member of typeb, otherwisefalse&&=- logical and assignment^^- logical exclusive-or (xor)^^=- logical xor assignment||=- logical or assignment
Scope
The following statements now have their own local scope:
for(including any variables defined inforinitializer orfor-initerator "binding")catch(including parameter)try,finallyif- in general, all block statements
Note: a variable will be "hoisted" (it will follow the scope rules of ECMAScript 3/AS2) when all of the following are true: it is defined in a var statement; the definition doesn’t specify a type; it has no attributes except true; the scope it would be hoisted into is not a class; Strict mode is not being used.
Strict mode
This is a more strict compile-time type checking mode. The following conditions are imposed by Strict mode:
- variables cannot be read or written to until they are defined (variables must be declared)
- variables cannot be hoisted (block-level scope is enforced)
- function parameters are checked for number and type (method call must match method signature)
- function definitions are constants rather than variables
- line-break semicolon insertion is turned off
- line breaks can be used in places where they previously weren’t allowed
Attributes (modifiers)
- Namespace (access) modifiers
internal- accessible within same package only-
private- now truly private (within class only) public- as in AS2- Visibility
enumerable- indicates a property can be seen usingfor-instatement (default is hidden)explicit- not shared via an import directive (see below)
- Class modifiers
final- class cannot be subclassed
- Member modifiers
static- as in AS2virtual- method or property can be overriden in subclassesfinal- method or property cannot be overridden in subclassesoverride- method or property overrides the definition from superclass. Can also be specified asoverride(true),override(false)(meaning it doesn’t override) oroverride(undefined)meaning it may override.
- Conditional
true- definition is processedfalse- definition is not processed
- Miscellaneous
prototype- function defines a class; this refers to function, and function can be used as a prototype-based constructorunused- definition is not used
- User-defined
- you can define your own attributes, either using a
constdefinition or other definition that defines a constant
- you can define your own attributes, either using a
Variables are public final by default; methods are public virtual by default.
Variables
- When a variable is declared, internally it creates a hidden variable and defines a getter and setter to access it — so a subclass can override a variable’s getter or setter (as long as the variable is declared as non-final).
Method (function) parameters
- Parameters are now required; if they are omitted the compiler generates an error unless the function is unchecked (see below).
- function parameters can be identified as optional by giving them a default value in the function definition, like
function(a:Number = 3, b:String = "hi"):String {} - optional parameters cannot come before required parameters.
- rest expression : If
...appears after parameters, additional parameters can be accepted. A single parameter name can be specified after...— it must be of typeArray, and any additional parameters passed to the function become elements of the Array, starting at index 0. This provides an improvement/replacement for theargumentsobject. - Unchecked functions: a function can become "unchecked", which is a backwards compatibility mode that is triggered when a function is not a class method, has no specified types (return or parameter types), and strict mode is disabled at the function definition. Unchecked functions have an
argumentsarray like in previous versions.
Classes
- can have inner classes (classes declared inside of other classes) — these must be declared
static thisalways refers to the instance (no more scope issues/need formx.utils.Delegate).- Overriding:
- Overriding a method without using the
overrideattribute results in a compiler warning (and using theoverrideattribute when there isn’t a method to override also results in a compiler warning). Parameters of the overriding method must match overridden method: the parameter type must match or be unspecified in overriding method (which defaults to the overridden method’s type), but default values for optional parameters may be different. - You can use
superto refer to the overridden method. - Methods can only override methods; variables can only override variables; getter/setter functions can override getter/setter functions or an instance variable.
- Overriding a method without using the
Packages
package {}statement - can surround class definition to define the package that the class belongs toimportstatement can be writtenimport pkg = com.mydomain.mypackageto create an alias for the package (this is useful if there are classes with duplicate names in separate imported packages — you can refer to them using the alias package name e.g.pkg.MyClass)Note: when I originally wrote this article, I had the order reversed for the import pkg = … statement (I had the package name first and alias after). After reviewing the spec again, the way it’s written here is (I believe) the correct one.
- The
importstatement can also point to a URL rather than a package name — the obvious use case for this is when a browser is the host, allowing you to import other classes (or other authors’ classes) from a central location.
Namespaces
This provides a mechanism to define certain portions of a code file — for example, certain methods of a class — as belonging to one or more namespaces. The use case presented in the spec is to provide a mechanism for creating multiple versions of code in a single file. You could define multiple versions of a method or property, or add new methods and flag them as only belonging to a certain version. Other code which refers to that code can specify which namespace (i.e. which version) of the code it is defined to use, and only the parts of the code which are flagged as belonging to that version (namespace) will be compiled.
I can see how this would be useful for the "importing other authors’ code from a url" situation — if the author updates the code, he/she can specify versions in the same file, and as long as your code points to the version it was created to work with, your code will never break. For ActionScript I don’t know if this is as useful — although I suppose this would allow authors (e.g. Macromedia) to create new versions of classes and still provide a single source file, and would avert the need for separate classpath folders like Macromedia had to do with Flash 8.
Predefined types
* Types marked with an asterisk were defined in ECMAScript 3 but not included in ActionScript 2
Never(the subtype of all types — the opposite ofObject)IntegercharTypeEvalError*RangeError*ReferenceError*RegExp*SyntaxError*TypeError*URIError*
There is no longer a distinction between objects and primitive types (all types are objects).
String(), Integer(), Number() are now explicit coercions rather than type conversion functions.
Machine types
These are types defined in the spec, but their names are not reserved words and can be used as identifiers.
sbyte(signed byte?)byte(8 bit integer)short(16 bit integer)ushortint(32 bit integer)uintlong(64 bit integer)ulongfloat(single-precision floating-point number)
Numeric literal suffixes: To specify a literal number of certain types, you need to add a suffix to the number (e.g. var x:long = 25L;). The suffixes are l or L for long; ul, uL, Ul, or UL for ulong; f or F for float.
Things I would like to see, which aren’t in the spec
Macromedia, please take note! You said you wanted to get developer feedback on the direction of ActionScript 3; well, here’s my first feedback:
Things I wish were added, but aren’t (at least, not in the standard)
- Items explicitly mentioned in the spec as considered but deferred:
protectedattribute- operator overloading
- For me, the priorities here would be the array access operator (
[]); being able to override/define implicit and explicit casts (type coercion); the equality (==) operator; then I guess the addition and subtraction operators. I haven’t ever had a need for any of the others, although I wouldn’t object to having them available.
- For me, the priorities here would be the array access operator (
- various typed Arrays (including
List,type[], andArray[type], among others)
- Items not mentioned at all:
- method overloading
- Obviously this is much easier to "fake" now with optional parameters and the rest expression, but sometimes it’s nice to have method overloads with different return types or completely different parameters.
- method overloading
Things included in AS2 but not in ECMAScript 4th edition
I expect Macromedia will keep these things around in ActionScript 3, and hopefully even add a few more nice things too!
- Interfaces - hopefully Macromedia will add support for properties/getters and setters, especially since now variables have an implicit getter/setter declared for them anyway, making the distinction between them trivial from the perspective of a user of the class.
dynamicandintrinsicclass attributes
You can leave a comment, or trackback from your own site.
29 Comments so far
Add your comment
Comment notes
Please keep comments on topic. Comments that are inappropriate or offensive will be edited or removed.
Paragraphs and line breaks are automatically converted to HTML, and quotation marks are converted to “smart” quotes.
The following XHTML tags can be used: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> . All others will be removed.
October 11th, 2005 at 1:43 pm
Peter Elst » What AS3 might look like … is reported to have said:
[…] like …
Posted by Peter on October 11th 2005 to Flash
Paul Robertson takes a detailed look at the ECMAScript 4 proposal which should giv […]
October 11th, 2005 at 3:09 pm
binaryatwork is reported to have said:
AS3 seems better than AS2.
However, there are still a few things in my wish list
1) real protected functions
2) pure abstract functions
3) better exception handling. AS2 does not throw exception at all and it is a nightmare to debug it.
4)
October 11th, 2005 at 4:41 pm
Ryan is reported to have said:
Thanks for taking the time to go through the spec and write such an informative article. I second the need for method overloading, having methods with multiple return types is hot. The rest looks great too! I guess it’s up to the lads to decide though.
October 11th, 2005 at 7:13 pm
Alan Shaw is reported to have said:
Let’s get started! Counting the days…
Totally agree about
protectedand operator overloading, especially +, -, +=, -=, *, *=, /, /=. Vector math!October 12th, 2005 at 12:21 am
JanuMedia :: ActionScript 3.0 Syntax :: October :: 2005 is reported to have said:
[…] 12, 2005 5:21 am ActionScript 3.0 Syntax Paul Robertson give us the the ActionScript 3.0 syntax preview. He looked details […]
October 12th, 2005 at 4:41 am
Joan | Garnet is reported to have said:
Primeras hipótesis acerca de AS3.0
Paul Robertson hace un repaso a lo que ActionScript 3.0 va a ser (o puede que sea). En el artículo se revisa el estado actual de ActionScript (AS2.0) y se hace una comparación con ECMAScript 4, que es a lo…
October 12th, 2005 at 5:09 am
ai829 is reported to have said:
cool~~~~”package” is my fovar favorite
October 12th, 2005 at 7:28 am
Tek is reported to have said:
I don’t see any mention in the ECMA 262-4 proposal to the possibility to create classes into classes. It is really something I want to be integrated in Flash because of the behavior of the compiler that understands only one class by file. It is a real pain to create a class file to declare a simple object with only variables in it for example.
October 12th, 2005 at 2:04 pm
Paul Robertson is reported to have said:
@binaryatwork:
On your #3, I’m hopeful that, assuming they include all the predefined error types, the built-in classes will actually throw errors rather than just ignore them. Parts of the ECMAScript spec do define that under certain conditions certain errors should be raised. Also, in at least a couple of the MM articles about AS3 I think they’ve mentioned that both compile-time and runtime errors will be improved. (Fingers crossed).
@Tek:
Look above at the section titled “Classes”. The first item says this:
“[you] can have inner classes (classes declared inside of other classes) — these must be declared static.”
I think that’s what you’re saying you want, right?
On a separate but related note, if MM implements the
packagestatement, it should theoretically be possible to define multiple classes (not nested) inside a single class file, and wouldn’t need to require that the folder structure match the package names. (Although admittedly with my C# code I avoid files with multiple classes and do make the folder structure match the code structure). I can see where those things would be particularly useful with a JavaScript library that was being loaded by a web browser — maybe they are not so necessary for AS.October 12th, 2005 at 2:20 pm
Pavel Simek is reported to have said:
I expect the “intrinsic” modifier no more being needed (except for just the built-in player API), since the new AS3 bytecode should contain all the type-checking and other information used by the compiler… What do you think? Are we going to include SWF/SWC directly into the classpath instead of intrinsic class files?
October 14th, 2005 at 1:35 pm
cogniblog » ActionScript3.0 Info is reported to have said:
[…] ctober 14th, 2005 at 1:35 pm (actionscript) I just stumbled upon this great article by Paul Robertson reviewing the changes in the upcoming (spring) rel […]
October 25th, 2005 at 9:11 am
Luca Deltodesco is reported to have said:
I deffinately agree for overloading, its such a pain with vector math or complex math or any such to have to use functions instead of operators
December 21st, 2005 at 8:31 am
ActionScript is reported to have said:
December 27th, 2005 at 11:18 pm
ActionScript is reported to have said:
December 27th, 2005 at 11:18 pm
ActionScript is reported to have said:
April 25th, 2007 at 3:04 am
Flashbattle.de Forum | ActionScript 3.0 | [ActionScript 3.0] ActionScript 3.0 (Linksammlung) is reported to have said:
April 29th, 2007 at 4:53 am
ActionScript & Flash Praxis :: Thema anzeigen - ActionScript 3.0 (Linksammlung) is reported to have said:
June 2nd, 2007 at 4:17 pm
AS 3 und Flash Player 8.5 - Flashforum is reported to have said:
July 2nd, 2007 at 6:24 am
NeoFlashers :: Ver tema - Action Script 3.0 FAAnswers is reported to have said:
July 11th, 2007 at 11:08 am
ActionScript & Flash Praxis :: Thema anzeigen - ActionScript 3.0 (Linksammlung) is reported to have said:
October 18th, 2007 at 10:50 am
無聊文章~~ is reported to have said:
[…] 2.0其實推出不算很久,相信很多人還在摸索中,但Action Script 3.0已慢慢出來了. ActionScript 3 Syntax preview 其實之前我已覺得寫Action Script 最難之處, […]
October 29th, 2007 at 4:41 am
www.gotoandplay.it :: View topic - - ActionScript 3 Syntax preview - is reported to have said:
November 12th, 2007 at 1:53 am
egoldy flashblog » Actionscript 3 is reported to have said:
November 23rd, 2007 at 9:55 pm
Actionsript 3.0 - Tanya@Putera is reported to have said:
February 5th, 2008 at 7:07 am
Shaun is reported to have said:
Hi there. This is an interesting article. I hate that operator overloading wasn’t introduced. It’s great for custom maths classes. Also, method overloading would’ve been nice for tidying things up a little. A bit annoying that these weren’t added.
It’s definitely an improvement on AS2, though, in terms of what it offers to us C/C++ immigrants.
February 15th, 2008 at 10:45 pm
NG BBS — Flash Player 9, Flash 9 Beta is reported to have said:
August 19th, 2008 at 9:28 am
Flash 9 - BLAZE Announced - Page 2 - Ultrashock Forums is reported to have said:
November 12th, 2008 at 7:02 am
ռflashվַ........... - վ - վ̳ - վ֮,վԴ,ݸվ,վ,վ,վ,վ,վ̳йվһɳ is reported to have said:
December 17th, 2008 at 8:37 am
FlashDevelop.org - View topic - Add "virtual" keyword support. is reported to have said: