haXe

Dynamic

When you want to get some dynamically typed behavior and break free from the type system, you can use the Dynamic type which can be used in place of any type without any compiler type-checking:

    var x : Dynamic = "";
    x = true;
    x = 1.744;
    x = new Array();

Also, a Dynamic variable has an infinite number of fields, all having the type Dynamic; it can be used as an Array for bracket syntax, etc...

While this can be useful sometimes, please be careful not to break your program safety by using too many dynamic variables.

Note that an untyped variable is of type Unknown and not Dynamic. That is, an untyped variable does not have a type until it is determined by type inference. A Dynamic variable has a type, an any type.

Dynamic Parameter

As it was said when listing standard library types, Dynamic can also take a type parameter. When you use Dynamic<String>, it has different behavior.

Dynamic<String> cannot be used in place of any other type. However, it has an infinite number of fields which all have the type String. This is useful to encode Hashtables where items are accessed using dot syntax :

    var att : Dynamic<String> = xml.attributes;
    x.name = "Nicolas";
    x.age = "26";
    // ...

Implementing Dynamic

Any class can also implement Dynamic with or without a type parameter. In the first case, the class fields are typed when they exist, otherwise they have a dynamic type:

class C implements Dynamic<Int> {
    public var name : String;
    public var address : String;
}
// ...
var c = new C();
var n : String = c.name; // ok
var a : String = c.address; // ok
var i : Int = c.phone; // ok : use Dynamic
var c : String = c.country; // ERROR
// c.country is an Int because of Dynamic<Int>

Dynamic behavior is inherited by subclasses. When several classes are implementing different Dynamic types in a class hierarchy, the last Dynamic definition is used.

Type Casting

You can cast from one type to another by using the cast keyword.

     var a : A = ....
     var b : B = cast(a,B);

This will either return the value a with the type B if a is an instance of B or it will throw an exception "Class cast error".

Untyped

One other way to do dynamic things is to use the untyped keyword. When an expression is said untyped, no type-check will be done so you can do a lot of dynamic operations at once :

    untyped { a["hello"] = 0; }

Be careful to use untyped expressions only when you really need them and when you know what you're doing.

Unsafe Cast

Untyped is pretty powerful but it allows all kind of invalid syntax on the right side of the untyped keyword. One other possibility is to do an unsafe cast which is similar to the standard cast except that no type is specified. That means that the cast call will not result in any runtime check, but will allow you to "loose" one type.

    var y : B = cast 0;

Cast is somehow the equivalent of storing a value in a temporary Dynamic variable :

    var tmp : Dynamic = 0;
    var y : B = tmp;

«« Packages and Imports | Advanced Types »»

version #1201, modified 2008-05-06 20:12:50 by ncannasse