lua-users home
lua-l archive

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

Do you mean that any code that has not explicitly declared a variable as "external" or "local" (or defined as a function parameter) ***and visible in the same source file*** must imply that "local" must apply by default ?

The problem may the definition of "file-level scope" : what can happen with code generators or code templates: these templates do not work if they can't use outer variables that are declared by other templates in the appropriate scope.

But for simple autonomous .lua files, this rule would be enough. Code template should not use the ".lua" extension as they are intended to be used within a preprocessor that will assemby and repack them into a runnable lua source "file". Such preprocessor may be for code generator, or it could be a "streaming" preprocessor (taking input from a continuous flow, e.g. a Lua I/O console, whose code is executed progressively as it is entered, while keeping the existing "local" variables and their values between multiple inputs of the same stream: the "file" level in that case is the whole stream, and not an isolated input; this is a special case where Lua code gets parsed, compiled and executed even if it is not complete...).

Le mar. 30 juil. 2019 à 08:45, Abhijit Nandy <> a écrit :

So we have quite a large Lua + Luabind code base and new comers from other languages often tend to forget the word local when declaring local variables in a block/scope. Of course the Lua static analyzer flags such code and we fix it, but sometimes such code still slips through basically because this particular warning is not an error at the moment.

What I am wondering is, apart from using the static analyzer to catch it, is there any code patch that can make variable declarations local by default?
Or is it too core to the language that changing this could cause issues elsewhere? I am willing to sacrifice performance as we are on PC and its worth doing this for us to prevent erroneous code if it can be done without requiring significant change to syntax.

I had a look at but there does not yet seem to be any solutions for Lua 5.3 for doing this globally in the language.