lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The current behavior in PUC Lua seems to be that modifying the loop control variable has no effect on the loop. I would not be surprised if LuaJIT was to apply optimizations that leverage the fact that you may not modify the loop control variable. Ideally the Lua compiler should emit a warning when it encounters modifications of the loop control variable. If you really need to modify the loop control variable, just create a local copy, which is more explicit, to spec and thus guaranteed to work, and just costs nothing but one line of code:

for i = 1, 10 do
     local i = i
     i = i + 42 -- perfectly fine
     print(i)
end

You can avoid shadowing the loop control variable (which linters like Luacheck don't like) by rename one of the two variables.