lua-users home
lua-l archive

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


Lexical scope was invented because dynamic scope makes programs
difficult to understand.  Lexical scope is a good thing, and it can be
combined nicely with modules.

David Kastrup <dak@gnu.org> wrote:
> Is there a top level?

Yes, but there need not be a *unique* top level.   There can be many,
and there is much confusion around this point.  To properly combine
lexical scope with a module system, each module should have its own
top level, and the "main" program is just the same -- it should have
its own top level as well.

What's wonderful is that you can have this in Lua without modifying
the language at all. [1]

Patrick Donnelly <batrick@batbytes.com> wrote:
> Personally I feel scripts shouldn't be able to modify a module's table;

It doesn't matter if a script modifies a module's table, as long as
the script has access only to a copy of the module's table.  However,
your suggestion is an equally valid approach.  A module loader could
create a table for the module that would enforce this (without
modifying Lua).  Sure, there would be performance implications, but
that's a different topic.

My module system, Darwin [1], imposes no time performance penalties
and only a small space penalty: some heap space is taken up by copies
of _G and other modules' binding tables.  It's not a lot of extra
space, and it could be eliminated if I had used read-only tables.  I
did not use read-only tables for two reasons:  (1) They impose a time
performance penalty because they must be implemented using an __index
function, and (2) some existing Lua code won't run that way, and I
designed Darwin to wrap existing Lua (and native) code, no matter how
it was written.

Jim

[1] http://lua-users.org/wiki/JimJennings
You knew this would be the reference, right?