Seems like an odd change, I wonder what the rationale is. Just making it clear to people new to the language that you can't adjust it mid-loop to change the loop count I guess?
Seems like an odd change, I wonder what the rationale is. Just making it clear to people new to the language that you can't adjust it mid-loop to change the loop count I guess?
The control variable in for loops is read only. If you need to change it, declare a local variable with the same name in the loop body.
Also [0]:
Roberto Ierusalimschy
> So what's the rationale to make them constant now? Does it have performance
> reasons?Yes. The old code does an implicit "local x = x" for all loops, in case you modify 'x'. With the new semantics, the code only does it when you ask for it.
https://www.lua.org/manual/5.3/manual.html#3.3.5
The loop count was fixed at the start of the loop. One of the reasons for this was performance. For loops behave differently if the step count is positive or negative, and it's a bit faster to compute that once, before the loop, than to repeat it every iteration.
I haven't run into this myself, but it does make sense, and eliminating this footgun sounds like a good idea.