I have a question about Lua, closures and local variables.
From my understanding, the loop variable in a numerical for-loop is implicitly local so I was expecting the following code to behave identical.
local a = 1
t1 = function()
return a
end
a = 2
t2 = function()
return a
end
print( t1() )
print( t2() )
table = {}
for i=1,2 do
table[i] = function()
return i
end
end
print( table[1]() )
print( table[2]() )
To my surprise, it did not. Why does the code print
2
2
1
2
and not
2
2
2
2
?