lua-users home
lua-l archive

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


I am relatively new to the Lua world but am using it heavily now and hope to
contribute here.  Your work is much appreciated and a lot of comments (mostly
quite positive) can be made about Lua, but I'll leave that for future posts on
this list.  I'd like to first just makes some comments about globals v.s.
locals, which seems to be a popular topic among the Luabites.

The terms "global" and "local" in Lua appear to be a misnomer.  One typically
thinks of the difference between the two as being that global variables have a
wider scope than local variables.  For example,

However, in Lua, this need not be the case at all.  A global variable may be
placed in an environment that is local to a single function (what you call
"non-global environments"), and a local variable can be placed at the top-level

The real difference seems to be that Lua's "local" variables have static/lexical
scoping (ref., resolved at
compile-time, while Lua's "global" variables do not.  Lua's "global" variables
rather have some type of quasi-dynamic scoping.  I don't think one would
precisely call it dynamic scoping (as in, for example,
Perl--, but the important thing is that the
scope is evaluated __at run-time__.  Lua also provides a fair amount of
flexibility in customizing the behavior of this scoping through __index and
__newindex (which, BTW, I thought might be more clearly named __getindex and
__setindex as in rawget and rawset, but I suppose some things might be too late
to change).  The downside of resolving at run-time is that it is less efficient.
 This is evident from the implementation in the Lua VM and is briefly noted in .  Therefore, using Lua "local" variables should
probably be the norm.  Still, locals have their limitations.  For example, the
VM significantly limits the number of locals, typically to 200
   For my own application, I'll probably extend the Lua VM slightly to allow
more flexibility in accessing static/lexical variables in C.

As a sidenote, it's interesting that in Perl, the (rarely used) keyword "local"
defines dynamic scoping, completely opposite to the usage in Lua!  That may be a
bit of a historical misnomer as well, as the above Perl link points out, "my
creates a local [i.e. static/lexical] variable. local doesn't."

Anyway, I thought I'd point this out--as in hinting a clarification to the
documentation or maybe a wiki page on this topic.  Thinking as a new user,
global v.s. local may be confusing, especially if one comes with a preconceived
notation from other languages.  I initially went down the wrong path, using
globals when I really wanted the efficiency of locals.  There is some further
discussion of Lua scoping in the below wiki page, but I believe it is out of
date (e.g. the section "Lua Currently Lacks Static Nested Scoping"):

I've also seen some discussion suggesting that the keyword "global" be added to
the Lua language, but, as mentioned here, there might be a better name for that
if you do make such a change.