lua-users home
lua-l archive

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


"Warlich, Christof" <christof.warlich@thermofisher.com> writes:

>> It is such an excessively bad idea that I would not care to see it as a
>> default in Lua.
> Hey, keep your pants on!
>
> No one ever said that one should use globals when locals are
> appropriate. It just happens to be that Lua variables are global by
> default with _one_ exception: loops.

And function parameters.  And table constructors.

So "it just happens to be that Lua variables are created local by
default with _one_ exception": assignment.

> And while there is no doubt that it is usually a very bad idea to use
> global loop variables, it is an equally bad idea to use global
> variables within functions.

"global" is only a fallback.  The default is to use the innermost scope
in which the variable has been declared.  That can turn into "global".
Since all "chunks" are local blocks, communication between chunks can
only be done using global variables.

> I was (and still am) just a bit curious to lean why it was decided to
> handle both cases differently, as a programming language is typically
> easier to learn when having to keep in mind fewer exceptins.

It is not like this has not been discussed to death on this list and the
Wiki.  So nobody else is curious, and you can get up to speed by looking
at the list archives and the Wiki.

-- 
David Kastrup