Separately photographed planes

What are semantics?

F = {} for i = 1, 10 do f[i] = function print(i .. " ") end end for k = 1, 10 do f[k] end

This prints the numbers from 1 to 10. In this case, i is closed over the value for each iteration of the outer loop. This is how I had always understood closures, and I was very happy...

And now I get the number 10 printed 10 times (let's forget that lua arrays are 1-based). It actually happens that in this case, the closure works over the variable, not its value, which makes a lot of sense, since I'm only calling the functions once the first loop is over.

JavaScript seems to have the same semantics (close over the variable):

var f = for (var i = 0; i < 10; i++) { f[i] = function { document.write(i + ' '); }; } for (var k = 0; k < 10; k++) { f[k]; }

Actually, both behaviors make a lot of sense, but are of course incompatible.

If there is a "correct" way to do this, then either lua, or c# and JavaScript are wrong (I haven't tried with other languages yet). So my question is: "what are the "correct" semantics of closing a variable inside a loop?"

edit: I'm not asking how to "fix" this. I know I can add a local variable inside the loop and close over that one to get the lua behavior in c#/JavaScript. I want to know what is the theoretically correct meaning of closing over a looped variable is, and bonus points for a short list of which languages implement closures in each way.

Related Posts