6.4 Runtime versus compile time type

We sometimes refer to a class or interface that helps to define the structure of a value as the value's type. What we really mean is that that value is a member of that class or interface type. This distinction is subtle but important. Since a value might belong to any number of unrelated types, to say that it is of a particular type is misleading.

In dynamically typed languages, expressions don't have types; they have values whose types may change each time the expression is evaluated.

Statically typed languages make the important simplification of associating a type with every expression, even if it is a very general one, when it is compiled. In this way, the suitability of an expression can be checked against its use before it is ever actually run. The cost of this added reliability is the loss of flexibility that comes from not having to think about the types of values.

function f( o : Object ) {
           var x : Number
           x = o           // Allowed in the standard dialect
       }
       f(10)                  // No problem, x gets set to 10

Other places where the differences between dynamic and static type checking can be seen are property access, and method invocation.

function f( o : Object ) {
           o.g()
           return o.x
       }

Whereas in a static type system, the binding for a method call or property read would need to be known at compile-time, the standard dialect always defers that checking until runtime.

The strict dialect has a hybrid type system. Normally, static type rules are used to check the compatibility of an expression with its destination type, but there are a few special cases. For example, when an expression on the right-hand side of an assignment expression consists of a reference to a property with no type, name lookup is deferred to runtime. When an object reference has a base object that is an instance of a dynamic class, the reference is checked at runtime. These dynamic typing features are useful when strict dialect programs are interoperating with dynamic features such as XML objects.


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/specs/actionscript/3/as3_specification56.html