lua-users home
lua-l archive

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


On 4/13/06, PA <petite.abeille@gmail.com> wrote:
> I was seriously contemplating re-implementing my class system in terms
> of the module function, but this global namespace pollution is a
> show-stopper :(
>
> Any reason(s) for 'module' littering the global environment?

Probably because this is what modules are for:

C: #include <math.h>
C++: #include <cmath>
Java: import java.lang.Math;
Python: import math

Though the semantics of the above vary greatly depending on the
language, all of the above "litter" the global namespace with a symbol
(or in the case of C, many symbols) in order to provide math functions
to programmer.

If Lua modules by default did not set a global variable, then the
idiomatic usage of 'require' would probably look something like
  local foo = require "foo"
whose benefits don't seem to be worth the extra verbosity.

If one of 'module' or 'require' must set a global variable, it seems
better that 'module' should do it, since then it is possible to create
modules that don't affect the global namespace, if that's what you
really want.  (Perhaps you want to create a module that when required,
instead of introducing code, changes the behavior of the language or
host application in some way, similar to "from __future__ import
division" in Python.)  'require' works fine even if the module being
required does not use 'module'.  Chapter 15 of blue PiL gives a few
examples of modules that don't use 'module'.

Why do you want to use 'module' for your class system?  If your goal
is to be able to 'require' classes, then you could write a 'module'
workalike that doesn't affect the global namespace.  If your goal is
only to get the enviornment setting effects of 'module', then the
problem is you're trying to use the function as something it's not. 
In either case, you probably should write your own function ('class'?)
that uses the same setfenv trick that module does.

My two cents,
Greg F