Saturday, October 4, 2008

No Block Scope in ActionScript

One of the things that I most liked about ActionScript when I first began to use it was how easy it was to pick up after using Java for several years. I have blogged about some of the similarities Java and ActionScript share, but there are also some differences (such as ActionScript's support for switching on Strings). In this brief blog entry, I will demonstrate one subtle difference in behavior in ActionScript from what I am used to from years of C++ and Java experience.

While ActionScript 3.0 supports global variables and constants and also supports variables and constants local to a function, it does not support block scoping. The "Variables" section of Chapter 4 ("ActionScript Language and Syntax") of Programming ActionScript 3 describes this in more detail. In this blog entry, I'll demonstrate this with a brief example.

VariableScopeTest.mxml

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="900" height="900"
applicationComplete="demonstrateVariableScope()">
<mx:Script>
import mx.controls.Alert;
const aConstant:String = "GlobalConstant";
var aVariable:String = "GlobalVariable";
private function demonstrateVariableScope():void
{
const firstConstant:String = "firstConstant";
var firstVariable:String = "firstVariable";
const aConstant:String = "LocalConstant";
var aVariable:String = "LocalVariable";
if ( 1 )
{
const secondConstant:String = "secondConstant";
var secondVariable:String = "secondVariable";
}
Alert.show(
"First Constant: " + firstConstant + "\n"
+ "First Variable: " + firstVariable + "\n"
+ "Second Constant: " + secondConstant + "\n"
+ "Second Variable: " + secondVariable + "\n"
+ "Global/Local Constant: " + aConstant + "\n"
+ "Global/Local Variable: " + aVariable + "\n" );
}
</mx:Script>
</mx:Application>


The output from running the SWF that results from compiling the above code looks like this:



This example demonstrates a couple important points about variable and constant scoping in ActionScript:

1. Like Java, local variables (variables local to a function) override variables of the same name on a more global level.

2. Unlike Java, ActionScript variables defined within a block are actually scoped to the entire function containing that block rather than to simply the block itself.



Where something like this can cause a little trouble is when a developer tries to use the same constant name in two different blocks. This will result in a compiler error ("Error: A conflict exists with definition ...") because the compiler will see the constants declared in two separate blocks within the same function as being an attempt to redefine a constant.

This is not a huge deal, but it is a subtlety that can be a surprise to a Java developer learning ActionScript.

3 comments:

Dazweeja said...

This same article is on techmemo.org but doesn't seem to credited to you anywhere on that site. Just thought you should know:

http://techmemo.org/2008/10/05/demonstrate-that-block-scoping-is-not-supported-in-actionscript-with-a-brief-example.html

@DustinMarx said...

dazweeja,

Thanks for letting me know.

Dustin

Rocksoccer said...

The biggest problem is not just you have give a different variable in different blocks. In the following case, it is very diffcult to find the error when you compile until the code actually runs.

if(a>b)
{
var varA:String="someString";
}

trace(varA);

But if you have block scoping, you already know the error when you compile.