You probably know this construct in some languages, like in PHP and 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 is often used in while loops, e.g. while (list($key, $val) = each($fruit)) echo "$key: $val;"
. But not only in loops.
In one line you could affect two or three variables coming from different columns of a row in a database, or taking scalar values from an array like in above example.
Another usage of it is in conjunction with split, especially in scripts:
list($a, $b, $c) = split("/", "papa/tango/charlie"); // this is PHP code again.
Is it possible to achieve that or something similar with Haxe?
For instance, supposing with had a static function assignTo (probably a macro), we could imagine this usage:
using ArrExt;
class Test {
public static function main() {
[13, "Bobby Fisher", "USA"].assignTo(age, name, country); // macro would *define* those vars, no declaration needed
trace(age);
trace(name);
trace(country);
}
}
I'm more looking for an Haxe 2.10 solution, but an Haxe 3 solution would be welcome to so it can be useful to everyone.