lua-users home
lua-l archive

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


I can't really answer for the creators of Lua, but here's why I consider
it a good design choice:

1. Scoping is complex. As programmers, we are obviously used to dealing
with scoping rules, but Lua isn't just for programmers; for someone who
doesn't care to learn any scoping rules or other programming concepts,
someone who just wants to maybe add a user-input to a script or modify a
calculation to change a sum to an average, it's easier to have the
default behavior be that setting a variable once means you can read if
after that point, no matter where you are in the program. If someone has
to ask their local IT guy why they set a variable in an if statement and
below it's suddenly nil, that's already more friction than necessary.

2. It makes scoping explicit; the point where you first use a dedicated
keyword is the level that the variable is limited to.

3. local means local. When you declare a local in a scope, it shadows
any variable of the same name in any outer scope, so you can be sure
you're not accidentally accessing anything you don't want to. If
variables were local to the outermost scope where they're used,
introducing a variable to some old code could accidentally change
another variable in an outer scope, or the other way around. With
explicit local, this would happen way less often and it makes it easier
for static analysis tools to catch on to the variable shadowing and give
you a warning.

4. You can just run your code in a "strict" environment easily, so it's
not like you have no means to defend yourself. It takes like 5 lines of
code to hack your scope so you get an error whenever you access an
undefined global (or overwrite an existing one, if you want that
additional safety).

So the only real problem with local by default is having to write
"local" a bunch of times; but code gets read more often than written
anyway, and "local" serves as a hint to the reader that this is the
first assignment of a variable, so it's yet another benefit.

That's my opinion anyway, for what it's worth :D

On 12/05/2020 17:50, Andrea wrote:
> I am trying to understand the rational for global by default and
> explicit local - I have been reading the lua wiki, lua archive, and
> all I could find on the topic
>
> I have seen that other languages have had similar discussion, in
> Python for example the proposal is to keep the local by default and
> use the "nonlocal" keyword
>
> now coming to Lua let's make an example copying from the nice article
> "why explicit local is a good thing" - I highlight in capital letter
> the keyword which is subject of the discussion
>
> ---- explicit local
>   LOCAL function getnums()
>      -- implementation omitted
>   end
>
>   loop:addtimer(1000, function()
>     LOCAL nums = getnums()
>     LOCAL square_sum = 0
>     nums:each(function(num)
>       LOCAL square = num*num
>       square_sum = square_sum + square
>     end)
>     print(square_sum)
>   end)
>
> ---- explicit nonlocal
>   function getnums()
>      -- implementation omitted
>   end
>
>   loop:addtimer(1000, function()
>     nums = getnums()
>     square_sum = 0
>     nums:each(function(num)
>       square = num*num
>       NONLOCAL square_sum = square_sum + square
>     end)
>     print(square_sum)
>   end)
>
> so it seems that local by default could be done - is there any real
> advantage in using LOCAL instead of NONLOCAL? 
>
> I am thinking about what happen when one omits the keyword; or
> thinking about what happens if one does not take into account
> declarations in enclosing scopes
>
> from a very broad point of view, when explicit LOCAL is used:
> local x = 1 -- always assign to local
> x = 1 -- maybe local, upvalue, global; it is global if x does not exist
> _ENV.x = 1 -- always global
>
> when explicit NONLOCAL is used
> x = 1 -- always assign to local
> nonlocal x = 1 -- maybe upvalue or global; it is global if x does not
> exist
> _ENV.x = 1 -- always global
>
> when referencing x in a formula nothing changes: x can be local,
> upvalue if no local is found with the sam enam, or global if no local
> and no upvalue has the same name.
>
> I take the opportunity to ask another question weakly related to this:
> I have been reading one of the design flaws in Lua is the fact that
> referencing variables that do not exist simply returns nil and no
> error is thrown
>
> but this seems to be wrong because if it is does not exist, when it is
> referenced the global environment is seached, and access to the global
> environment can be monitored by metamethods, that is strict.lua - am I
> right?
>
>    Andrea
>
>
>
>
>
>
>
>
>
>
>
> -- 
> Andrea Vitali
>
>
>
>
>
>
>
> _______________________________________________
> lua-l mailing list -- lua-l@lists.lua.org
> To unsubscribe send an email to lua-l-leave@lists.lua.org
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org