2 ms·
> I think it comes down to if you think it makes sense for variables to be captured by value or by reference in closures. No, this comes down to “should a loop
by dragonwriter 4y ago
> I think it comes down to if you think it makes sense for variables to be captured by value or by reference in closures.
No, this comes down to “should a loop control variable be scoped to the block—or in python’s case function—the loop is in and updated with each iteration or a fresh variable scoped to each loop iteration that happens to share the same name.”
- randomswede 4y agoIn the "the variable lives in the function scope", the answer is unequivocally "it should be bound once and updated", if it only exists in the scope of the loop function, both "it is bound on each iteration" (and thus safe to close over without surprise) and "it is bound once and updated" are valid answers, but I have a preference for the first, but many languages actually choose the second.