Extern classes can implement Dynamic
and Dynamic<T>
which enables arbitrary field access. In the former case, fields can have any type, in the latter, they are constrained to be compatible with the parameter type:
extern class ImplementsDynamic implements Dynamic<String> { public var present:Int; public function new(); } class Main { static public function main() { var c = new ImplementsDynamic(); // valid, present is an existing field c.present = 1; // valid, assigned value is a String c.stringField = "foo"; // error, Int should be String // c.intField = 1; } }
Implementing Dynamic
does not satisfy the requirements of other implemented interfaces. The expected fields still have to be implemented explicitly.
Trivia: Implementing
Dynamic
on non-externsStarting with Haxe 4, implementing
Dynamic
is only allowed on extern classes. In previous versions any class could implementDynamic
, allowing arbitrary fields to be read or written. Additionally a specialresolve
method could be used to resolve read access to a non-existend field. A similar feature is now available as field access operator overload on abstracts.