[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Numeric for loop: Changing control variable REALLY dangerous?
- From: Francisco Olarte <folarte@...>
- Date: Mon, 12 Dec 2022 13:44:20 +0100
On Mon, 12 Dec 2022 at 12:06, Lars Müller <firstname.lastname@example.org> wrote:
> 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
This does not MODIFY the loop control variable. It only lets you have
> You can avoid shadowing the loop control variable (which linters like Luacheck don't like) by rename one of the two variables.
... as you clearly state. If you NEED to modify the loop control
variable just byte the bullet:
while i<=10 do