Sunday, January 27, 2008

Actionscript 3: Strict-Mode Versus Standard-Mode Compilation

ActionScript offers two different modes for compiling a program: strict mode and standard mode. In strict mode, the compiler reports more errors than in standard mode. The extra strict-mode errors are intended to help programmers locate potential sources of problems in a program before the program actually runs. Strict mode is, therefore, enabled by default in all of Adobe's compilers. Programmers who wish to use ActionScript's dynamic features, or who simply prefer to solve problems (i.e., debug) at runtime rather than at compile time can choose to compile using standard mode.

The following questionable acts of programming will cause a compiler error in strict mode, but not in standard mode:

  • Supplying the wrong number or types of parameters to a function

  • Defining two variables or methods with the same name

  • Accessing methods and variables that are not defined at compile time (but might be defined at runtime using the dynamic features techniques)

  • Assigning a value to a nonexistent instance variable of an object whose class is not dynamic

  • Assigning a value to a constant variable anywhere other than the variable's initializer or, for instance variables, the constructor method of the class containing the variable's definition

  • Attempting to delete (via the delete operator) an instance method, instance variable, static method, or static variable

  • Comparing two incompatibly typed expressions

  • Assigning a value to a type-annotated variable where the value is not a member of the declared type (some exceptions to this rule exist, see the special cases of Strict Mode below)

  • Referring to nonexistent packages


Strict Mode's Three Special Cases

There are three situations in which the compiler ignores type mismatch errors in strict mode, deferring possible type errors until runtime:

  • When an untyped expression is assigned to a typed variable or parameter, or returned from a function with a declared return type

  • When any expression is assigned to a typed variable or parameter whose declared type is Boolean, or returned from a function whose declared return type is Boolean

  • When any numeric value is used where an instance of a different numeric type is expected

No comments: