Haxe construct to mimic PHP list() = each() in Haxe 2.10 -
you know construct in languages, in php , perl:
var $a = ["apple", "banana", "pear", "orange"]; list($p1, $p2, $p3, $p4) = each($a); echo $p1; // apple echo $p2; // banana echo $p3; // pear echo $p4; // orange it used in while loops, e.g. while (list($key, $val) = each($fruit)) echo "$key: $val;". not in loops.
in 1 line affect 2 or 3 variables coming different columns of row in database, or taking scalar values array in above example.
another usage of in conjunction split, in scripts:
list($a, $b, $c) = split("/", "papa/tango/charlie"); // php code again. is possible achieve or similar haxe?
for instance, supposing had static function assignto (probably macro), imagine usage:
using arrext; class test { public static function main() { [13, "bobby fisher", "usa"].assignto(age, name, country); // macro *define* vars, no declaration needed trace(age); trace(name); trace(country); } } i'm more looking haxe 2.10 solution, haxe 3 solution welcome can useful everyone.
the feature called destructuring assignment, or unpacking. instead of writing macro function, there simpler way... use pattern matching (in haxe 3)!
switch ["apple", "banana", "pear", "orange"] { case [p1, p2, p3, p4]: trace(p2); //"banana" } it looks different, functionally same.
Comments
Post a Comment