| 
         | 
    ||
        
  | 
    
Rici Lake wrote:
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
end
Then the right thing is the behavior of Lua 5.0.2 (in fact, this is the 
way for is explained in the reference manual :-) ). You can easily have 
it the other way by explicitely declaring another local:
timestable = {}
for i = 1, 10 do
  local k = i
  timestable[i] = function(j) return k * j end
end
The new way just makes it more convenient. Well, I'm sure this new way 
will be documented in the 5.1 reference manual. :-) And the loop 
variable is not really created anew each iteration; the same register is 
reused. There is no performance penalty.
-- Fabio Mascarenhas