|
|
||
|
The real index name is the last localvar; it is not in scope until the loop starts. Consequently, for loops correctly (imho) create a new index object on each loop, which in turn means that upvalues work correctly.
In other words:
timestable = {} for i = 1, 10 do timestable[i] = function(j) return i * j end end
will do the right thing in lua 5.1, and something quite different in earlier versions.
Depends on how you interpret the for loop. If you interpret the loop as:
do
local i = 1
repeat
timestable[i] = function(j) return i * j end
i = i + 1
until i > 10
endtimestable = {}
for i = 1, 10 do
local k = i
timestable[i] = function(j) return k * j end
end-- Fabio Mascarenhas