Static extensions in Haxe can be used to mimic multiple inheritance similar to mixins in Javascript. It’s a powerful feature and should be used carefully.
StringTools class is a good example of station extension. Refer to my earlier post for more info on that.
Quick Example:
using
keyword brings the defined class into the context.
Another example with multiple static extensions:
A few things to note:
- If the same method is defined in multiple static extension classes,
using
expressions are checked from bottom to top. For example if the methodlast
is also defined inArrayUtils2
then it will take precedence asArrayUtils2
is the bottom class in the above example. - Built in class fields would take priority over the static extension fields. For example if you define
toString
method inArrayUtils
class above it wouldn’t work astoString
is a built in array method and it takes precedence. - Compiler metadata
@:noUsing
can be used in static extension class if you want to omit any field being included in the context. Check the example below.
You can’t use the method second
as the compiler will not include it in the context when used.