AS2 OOP: Changes and Additions
by senocular
Strict Data Typing
To further aide debugging and assurance that you
are in fact using your variables as you had intended
for them to be used, ActionScript 2.0 has introduced
strict data typing to the language. What this
is, is the ability to specify what type a certain
variable is when it's created (Number, String,
MovieClip, etc.). Then, should you ever attempt
to try to use it as a variable of an alternative
type, an error will occur thereby isolating a confliction
helping you to debug.
Strict data typing
also allows for Flash to associate a variable
with an object to help you with code hints when
using that object. Before, this was only possible
using underscore suffixes in your variable naming.
Now you have the option of using strictly typed
variables instead, allowing for more room in your
own naming conventions. (Note: XML definitions
will still be needed for code hints to function
properly.)
Typing is always placed following a variable or function declaration. For variables, the type is placed immediately after the variable name separated by a colon (:). For functions, placement directly after the parameters list, also separated by a colon. Function types here, however, refer to the type of the value that the function is returning. If the function has no return, Void is used. Also, for functions, the parameters used can too be strictly typed.
var variableName:Type = value;
function functionName(parameter:Type):returnType {}
The following is an example of some variables and a function being strictly typed. Notice that when a type is given a value not matching its data type, an error occurs.
- var name:String = "Joe";
- var age:Number = 21;
- function message(msg:String):Void {
- trace(msg);
- }
- message(name);
- message("Bob");
- message(age);
Types
are represented by using the object or class name
of the type being represented. This includes your own custom classes
as well as interfaces too (interfaces are considered a data type). Because age was of a type Number and not String, there was that Type mismatch error.
Variable types are not set in stone. Should you ever need to change the type of a currently typed variable, just re-declare the variable with var and retype it. That's all; its not as restrictive as it may initially seem. Example.
- var value:Number;
- value = 10;
- var value:String;
- value = "ten";
Overall, this style
of specifying type is very similar to the underscore naming convention
(name_str vs. name:String) having come out of Flash MX. Only in this case, the
type is not embedded within the variable name itself.
It's used only in that single instance of declaration.
Outside of that, the variable is used normally
without any other real indication of type. This may be beneficial, or not. You may like having the variable name show its type. Either way, strict data typing in this manner also allows for code hints in using variables not defined in the underscore suffix convention.
[
code hinting from strict data typing ]
The use of strict data typing is only for ActionScript 2.0. If you use it with ActionScript 1.0, you won't get any errors (and you'll get code hints) but the variables its was used with won't function properly.
Some programming
languages attain improved performance when using
strict data typed variables. That is not the case
with ActionScript. Strict data typing offer no
advantages at runtime. They serve only as indicators
for the compiler checks and for code hints in
the authoring environment.
It may seem like a lot of extra work to set this up, but
strict data typing is not required. It does, however, help you manage your variables. It's also good programming practice so it's recommended that you use it.
|