While Haxe has a static type system, this type system can effectively be turned off by using the
Dynamic type. A dynamic value can be assigned to anything, and anything can be assigned to it. This has several drawbacks:
It is very easy to come up with examples where the usage of
Dynamic can cause problems at runtime. Consider compiling the following two lines to a static target:
var d:Dynamic = 1; d.foo;
Trying to run a compiled program in the Flash Player yields an error
Property foo not found on Number and there is no default value. Without
Dynamic, this would have been detected at compile-time.
Trivia: Dynamic Inference before Haxe 3
The Haxe 3 compiler never infers a type to
Dynamic, so users must be explicit about it. Previous Haxe versions used to infer arrays of mixed types, e.g.
[1, true, "foo"], as
Array<Dynamic>. We found that this behavior introduced too many type problems and thus removed it for Haxe 3.
Dynamic should be minimized as there are better options in many situations, but sometimes it is just practical to use it. Parts of the Haxe Reflection API use it, and it is sometimes the best option when dealing with custom data structures that are not known at compile-time.
Trivia: Dynamic in the Standard Library
Dynamic was quite frequent in the Haxe Standard Library before Haxe 3. With the continuous improvements of the Haxe type system, the occurences of Dynamic were reduced over the releases leading to Haxe 3.