lua-users home
lua-l archive

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




Le jeu. 15 nov. 2018 à 21:19, Dibyendu Majumdar <mobile@majumdar.org.uk> a écrit :
On Thu, 15 Nov 2018 at 11:55, Philippe Verdy <verdy_p@wanadoo.fr> wrote:
Hi, to be honest the for num loop variable should be treated as
'readonly' inside the loop body

Not needed because you say yourself:
 
as it is a copy of the real index
variable, and changing it inside the body doesn't affect the loop,

This means that the value provided by the loop iterator can be transformed as you want to do the operations needed in your loop, you don't necessarily need to keep it unchanged.  A compiler can detect if it's assigned or not; and if not, it can suppress the copy into from the actual control variable (invisible, but not readonly) to the local variable.
and
is just bad practice in my view.

Given it is local, this is not bad practice at all, it's jsut a standard local variable, preinitialized, but still mutable as we want. So you don't need to declare another local varaible in the loop
 
Anyway I take the liberty of
optimising the for num index here, and require that the variable is
not assigned a non-integer value. I would forbid assignment but this
is harder to do.

The best you can do is to emit a warning because it may not be compatible with older versions of Lua. If you compile for Lua 5.3 only, nothing is needed, the actual control varaible is well protected, and it's just simpelr to manage all variables the same way everywhere in the code inside the loop.

 
Regards
Dibyendu