-
Notifications
You must be signed in to change notification settings - Fork 0
Smart Variables
Smart Variables is an language extension, that brings a more concise syntax of variable initialization. Sometimes the singular syntax of the variable declaration statement in ActionScript3 may seem a little bit redundant and too much verbose. Smart Variables extension adds an optional opportunity to create new variables in a compact style (not providing a keyword var or a type).
This is particularly useful in conditional expressions (if-then-else constructs) or in complicated objects description (together with ObjectBuilder language extension).
Moreover, as opposed to the variables of the wildcard type, the Smart Variables are context-oriented. On creation of a new Smart Variable, its type will be automatically determined from the context. That means, type system checks and autocomplete are enabled with Smart Variables.
// without Smart Variables
var s : Sprite = this;
if (s is MovieClip) {
var mc : MovieClip = s as MovieClip;
if (mc.currentFrame > 1 && mc.totalFrames > 10) {
}
}
Using Smart Variables avoids the two issues:
- excessive variables creation;
- groundless duplication of code.
// with Smart Variables
var s : Sprite = this;
if (s is MovieClip && (mc:(s as MovieClip)).currentFrame > 1 && mc.totalFrames > 10) {
}
##Syntax
###variableName : expression
To initialize a single variable, that will be use only once in the whole code, you have only to put a colon and a name before any expression. Smart Variables are called so because they can determine the type of an expression from the initializer.
var s : String = "hello"; // a variable
s:"hello"; // a smart variable
Please use this construct with care. Despite of its compact view, placed outside of conditional expression or object initialization with ObjectBuilder, it can reduce the altogether code readability.
##Using Smart Variables with ObjectBuilder
As well as ObjectBuilder, Smart Variables are inspired by the syntax of functional languages such as JavaFX, Scala or Ruby. The combination of these two language extensions makes your code more concise and readable.
sp:Sprite{
width : 100;
height : 100;
};
this.addChild(sp);