lua-users home
lua-l archive

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


Pierre Chapuis wrote:
> So what do we (or at least I) mean by "don't pollute the global namespace"?
> We mean that you should not create globals in *library code*.
[...]
> I don't care what you do within your own
> codebase, but libraries should not break other code when required.

I strongly agree with this point, a library should never write globals
unless explicitly instructed to do so and it is good when all the
dependencies are listed at the top.

In regard to the quote from the original post where it was said that
globals should be completely replaced by upvalues, I can think of a
situation where upvalues can actually be harmful and globals (or sandbox
environments or any table lookups) are the more useful way:

One of my first exercise projects with Lua was starting the development
of a server for Multi-User Dungeons (text-based role-playing games).  A
MUD server usually runs for many months without even an application
restart so it is extremely important to be able to swap code parts in
the running application.

Once a function is stored somewhere as an upvalue it is very hard to
change its code, so all functions should be accessed through table
lookups and on reload the existing tables are updated.  The way to find
out whether a table already exists usually involves the global namespace
(or the sandbox in which the script is loaded).

    my_room = my_room or {}

    function my_room.open_the_door()
      ...
    end

I guess this situation extends to many applications where live coding is
desired.

Best regards,

David Kolf