Wednesday, July 18, 2007

ActionScript 3, Undocumented Features?

You have been entrusted to be in on some secret features of ActionScript 3.  Only a handful of programmers around the world will see this information.  This is mostly due to the fact you have been able to hack your way into one of the most obscure blogs on the planet!

Actually, I don't know if I am breaking new ground here but I haven't been able to find any mention of this feature.  I really discovered it sometime ago while writing an earlier post but I didn't know the extent of the find.

And the find is fairly extensive but I've come up with only a few practical uses and one of those was already discussed in the mentioned post.  I don't know how far back these features go.  It may be they were around before ActionScript 2; before I ever used Flash.  Maybe veteran Flash developers find it so ho-hum they forget to pass it on to newbies.  I haven't tried this with AS2 because I don't want to fire up Flash again and have to re-learn anything.  BE WARNED: Be careful how you use undocumented features.  Also, "undocumented" means I haven't been able to find anything about them in the Adobe documentation.

Please let me know if you can find this someplace.  Someone needs to write a book about how to best use and find available resources for Flash, Flex and ActionScript.

The Secret Features:

You can insert multiple comma separated expressions when the syntax calls for an expression between parentheses.

For example:

do
{
    // do something
} while( a = b, a == 0 );

The while test will be performed on the rightmost expression, a == 0, but variable a will be set to the value of b prior to the test.  You are not limited in the number of expressions but you are limited in the type of expression.  For example, you cannot declare a variable with a var and you can't use an if statement.  The expression must resolve to a value.  The ? : expression can be used and you can call a function.

The do while statement, the while statement and the E4X filter predicate expression are the best candidates for this feature.  However it can also be done in the return and switch statements although I can't see any value in using it.  The return statement doesn't even require parentheses.

I was hoping to show a compelling example using do while or while but haven't been able to think of one.  I made this discovery when I was creating a loop and using IViewCursor to iterate over an XMLListCollection.  The where clause was getting too long to fit into a line and I wanted to break it up.  So I thought, what the heck, why not try it.

You can also do something similar with the for statement:

for( I = 0, N = 2; N < 5,I < 3; I++, N++ ){}

I think I recall seeing examples of the for statement before, but I don't remember if it was for ActionScript.

I guess when the ActionScript parser encounters the open parenthesis it handles it as though it is a function call with arguments without paying too much attention to the context.  It's okay, I won't tell Adobe if you won't.

3 comments:

Rafagd said...

It's no soooo secret as you think.

The programming language C has this features since ages ago... But people try to not use it because it can turn the code illegible.

Jim Freer said...

Thanks Rafagd for your comment. The secret part is due more to the obscurity of my blog which you just confirmed by submitting the first comment for a nearly year old posting. (Thanks again.)

Many years ago when C compilers were just emerging for microprocessors I was happily writing code with Intel's PL/M. It is my all time favorite language and probably the best time of my career. Later on, when micro-based C compilers became mature, I resisted working with C programmers and projects. I didn't like all the fancy shortcuts which were considered pretty much mainstream C. I don't like having to decompile a line of source code to figure out what it is doing.

So I'll have to agree with you but I don't even like a lot of the pretty standard stuff in C. Oh, I've written lots of lines of code in C but my C code looks pretty much like my PL/M code, or Fortran, Cobol, C++, Java, VB, C# or even ActionScript.

My long time programming partner tried to get me interested in Perl but I just didn't like it. Unfortunately if I had only followed his advice I'd probably be employed today.

Thanks,
Jim Freer

Anonymous said...

Well its feb 1st 2010. Its almost 3 years since the post. Anyway thanks for posting.