What is the `using` keyword in Haxe? -
i see people use keyword using
in haxe code. seem go after import
statements.
for example, found code snippet:
import haxe.macro.context; import haxe.macro.expr; import haxe.macro.type; using haxe.macro.tools; using lambda;
what , how work?
the "using" mixin feature of haxe referred "static extension". it's great syntactic sugar feature of haxe; can have positive effect on code readability.
a static extension allows pseudo-extending existing types without modifying source. in haxe achieved declaring static method first argument of extending type , bringing defining class context through using
keyword.
take @ example:
using test.stringutil; class test { static public function main() { // possible because of `using` trace("haxe great".getwordcount()); // otherwise had type // trace(stringutil.getwordcount("haxe great")); } } class stringutil { public static inline function getwordcount(value:string) { return value.split(" ").length; } }
run example here: http://try.haxe.org/#c96b7
more in haxe documentation:
- haxe static extensions in haxe manual
- haxe static extensions tagged articles in haxe code cookbook
Comments
Post a Comment