Type checking in ActionScript 3.0

The Type checking can occur in time of compilation or time of execution. The languages static Type , such as C++ and Java, they make the verification in compilation time. As a language dynamically Type , the Action Script 3,0 verifies in runtime. Linguages dynamically type offer to flexibility when structuralizing your code. For example, the following code not specifies the data type of the parameter “xParam”. In runtime, the parameter is used for to get the value of the type Number and one value of the type String. In function dynamicTest() is used the operator is for to test if the parameter is of the type Number our String

ActionScript

  1. package {
  2.     import flash.util.trace;
  3.     import flash.display.Sprite;
  4.     public class Checktype extends Sprite {
  5.         public function Checktype(){
  6.             this.dynamicTest(100);
  7.             this.dynamicTest("uma cadeia de caracteres");  
  8.         }
  9.         public function dynamicTest(xParam):void{
  10.             if(xParam is String){
  11.                 var myStr:String = xParam;
  12.                 trace("String: "+myStr);
  13.             }else if(xParam is Number){
  14.                 var myNum:Number = xParam;
  15.                 trace("Number: "+myNum);
  16.             }
  17.         }
  18.     }
  19. }

Outher example of the offered flexibility offered involves the use of the properties and methods that they are not known in compilation time. The compiler of the ActionScript 3,0 allows that you he uses properties and he calls methods that do not exist in compilation time. This allows that you it creates properties dynamically or it attributes methods in runtime. For example, the following code creates a called function runtimeTest () that it invokes a method and it returns a property, none known the compiler. The code will not generate an error of the compilation time, but if the property or the method will not be accessible in execution time, an error in execution time will occur.

ActionScript

  1. function runtimeTest(xParam){
  2. xParam.someMethod();
  3. return xParam.someProperty;
  4. }

to declare a data type for an variable, it uses the attribute var when declaring the variable and adds the operator colon (“:”) followed for the type of data as a suffix to the changeable name. To associate a data type with a parameter, you the operator uses colon (“:”) followed for the data type. For the example, the following code adds the information of the data type to the parameter of xParam, and declares it one myParam changeable with an explicit data type:

ActionScript

  1. function runtimeTest(xParam:String) {
  2.     trace(xParam);
  3. }
  4. var myParam:String = "hello";
  5. runtimeTest(myParam);

More:
Type checking Action Script 3.0

Para saber mais:
Adobe Labs

C data types

Java data types

ActionScript 3.0, Flex, Principal

1 comment


  1. Thanks for this post 🙂

Leave a Reply