3 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. Which ever makes the most sense depends
by duped 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.
Which ever makes the most sense depends on the context of a program which is why in languages that have both reference and closure semantics you can choose how the variables are captured. When that isn't the case you need to pick for someone, and it gets weird.
- 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.