Every programming language has keywords and AS3 has lots of those. They are even categorized in three groups. Have a look at my favorite website but now under the topic: Keywords and reserved words

You see three categories:

  1. first says – don’t you dare to use me in your code in different context as I meant to be
  2. second says – wellll, I am a keyword but only in some context, so do your best
  3. third says – I am not a keyword, but I could be – don’t you touch me

So, our conclusions – this code is ugly but correct:

?View Code ACTIONSCRIPT
function get get():int {
    return 0;
}
var namespace : Namespace;

Another logical conclusion from the first group is that packages should not be named as keywords. Because if you have a class in such a package than a keyword will appear in your code as identifier in an import statement. Sadly logic is relative from creative point of view. You can name a sub package “default”:

?View Code ACTIONSCRIPT
import my.default.MyClass;

But on the over hand this won’t compile:

?View Code ACTIONSCRIPT
import default.MyClass;

:)

AS3 is a proud holder of three inline languages:

  • XML
  • E4X
  • RegEx

I posted a references to usage of E4X in previous post. And here are two reference for usage of RegEx:

http://donttrustthisguy.com/2008/02/29/utilizing-regular-expressions-in-as3/

http://gskinner.com/RegExr/

But what I would really like to show you in this post is some syntax wonders of inline XML. Have a look at this code:

?View Code ACTIONSCRIPT
<root/*an AS3 comment*/>
    <!-- XML comment--> some text
</{(trace("let's close this tag"), "root")}>

It is compilable code. And yes you can use AS3 comments and XML comments inside of inline XML.
You can also embed inline AS3 inside of inline XML and it will be evaluated at runtime. So the close tag is correct in this example.

I hope the parenthesis syntax is familiar from the previous post.

Parenthesis has a really interesting field of application in AS3.

Have a look at this page: Parentheses

The interesting one is to evaluate a series of expressions:

?View Code ACTIONSCRIPT
var a:int = 2;
var b:int = 3;
trace((a++, b++, a+b)); // 7

Very cool stuff it looks like an inlined function. Normally I would write something less creative like this

?View Code ACTIONSCRIPT
function foo(a:int, b:int):int{
    a++;
    b++;
    return a+b; 
}
trace(foo(2,3)); // 7

I also would say that second version is more readable and reusable, but not as cool as the first one.

Another interesting thing about this notation is that you can’t declare variables inside parenthesis, but you can declare anonymous functions:

?View Code ACTIONSCRIPT
var f:Function;
trace((f = function():int{return 7;}, f())); // 7

And that’s not all folks. There is another context where parenthesis have a pretty cool role  - E4X. In E4X you use parenthesis for “filters” declaration.

There are a few nice posts about E4X filters on this blog: http://joshblog.net/2007/05/08/methods-to-filter-data-with-e4x-in-flash-9/

This will be a sarcastic run of posts about AS3 syntax and language features.

I must say my last big ActionScript project was a few years ago and now I am not an ActionScript developer anymore. I learned AS3 from the Tool Developer point of view, but neither the less I was testing it for some small efforts (BehaveAS).

As tool developer you learn some crazy stuff that no “using” developer would know. This crazy stuff will be posted here, maybe somebody will find it interesting.

© 2011 Max Blog Suffusion theme by Sayontan Sinha