2 ms·
> Is it literally just the language invisibly adding the parent-scope variables and their values to the top of my function? I believe that would be 'lambda-lif
by mrkeen 7y ago
> Is it literally just the language invisibly adding the parent-scope variables and their values to the top of my function?
I believe that would be 'lambda-lifting', i.e. taking a lambda which has free variables, adding those free variables to its input variables, moving the lambda out into the top- level, and modifying the call site to pass in the no-longer-free-variables. That should give you completely reasonable lifetimes and only require the stack.
It gets trickier when you want functions to be able to return functions. For instance:
add(a, b) {
a + b
}
myFun() {
var add5 = x -> add(5, x);
var add6 = x -> add(6, x);
add5(1) + add6(2)
}
In this case I've made two variants of add. I can't just run add5 on the stack, get rid of it, and then run add6 afterwards, because they both need to be alive at the same time.