lua-users home
lua-l archive

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

Bryan White <> wrote:
> David Given wrote:
> > They actually work by totally different mechanisms. Lua locals are
> > lexically scoped true variables, and exist on the Lua stack[1]
> > internally. When you access a local, it's referred to directly[2]
> > without any table lookup happening at all. Lua globals aren't really
> > variables at all; they're simply syntactic sugar for doing table
> > accesses on _G. This:
> It seems that asking script coders to be aware of such issues and 
> optimize their code for them is less than optimal.

As David pointed out, "global" is a clear misnomer here. The Lua
documentation could usefully give better explanations as well as some
guidance as to what "local" and "global" really means in a Lua context
(and how both concepts could/should be used -- or not used). Someone
coming from other languages (or even someone with no preconceptions)
won't necessarily spot the interesting pitfalls hidden in these
innocent-sounding words.

Many, if not most, of my scripts these days don't use globals at all.
Not mainly because they're slightly faster that way, but because they're
easier to understand and less error-prone.

cheers  thomasl