I’ve occasionally used a trick to embed a recursive function inside another function (just to keep scope/etc). Here’s a simple (and pointless) example:
class Test {
static function main() {
var items = new Array<Int>();
function doRecurse(count:Int) {
items.push(count);
if(count > 0)
doRecurse(count-1);
}(5);
trace(items);
}
}
Kind of like a fold operation, but there can be more complex code/branching within the recursion. The item array pops out the other side with all the data I want without having to pass around extra variables.
This quit working with Preview 5. Not a big deal, but maybe something to check on?
class Test {
static function main() {
var items = new Array<Int>();
var doRecurseFunc = function doRecurse(count:Int) {
items.push(count);
if(count > 0)
doRecurse(count-1);
};
doRecurseFunc(5);
trace(items);
}
}
This doesn’t answer your question, but it seems worth mentioning anyway: you can call named local functions directly, without assigning them to a temp var first:
class Test {
static function main() {
var items = new Array<Int>();
function doRecurse(count:Int) {
items.push(count);
if(count > 0)
doRecurse(count-1);
}
doRecurse(5);
trace(items);
}
}
Something seem to have changed at expression level, leading h4p5 to consider that function doRecurse(count:Int) { ... } and (5) are two separated instructions, as if separated by a ;.
var e = macro {
function doRecurse(count:Int) {
items.push(count);
if(count > 0)
doRecurse(count-1);
}(5);
};
var printer = new haxe.macro.Printer();
trace(printer.printExpr(e));
Outputs:
Macro.hx:16: {
function doRecurse(count:Int) {
items.push(count);
if (count > 0) doRecurse(count - 1);
};
(5);
}
It would not have occured to me to be using this syntax, but it’s clearly unexpected behavior. @Simn, what do you think?
This was a deliberate change to be consistent with other occurrences of }. However, I agree that it looks quite silly in your example, so I’m not sure what to do here…