lua-users home
lua-l archive

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


Roberto Ierusalimschy wrote:
> I guess you can fix the two problems without the downside by making
> "module" do not inherit globals into the new environment.


This would strongly encourage (well: 'force') writing modules
with proper caching of globals. Good thing, IMHO.

It has another advantage: the way module() currently sets up
the environment for all functions in a module, there is an
extra indirection for all GETGLOBALs (i.e. it's slow).

In general there are two ways to get everyone to do something
the 'right' way: make the wrong way inconvenient (or impossible)
or make the right way more convenient.

Right now all suggestions go for the first alternative.
If there was a standard preprocessor for Lua, the obvious
thing to do would be to define the following substitutions:

import "a.b.c"
local c = require "a.b.c"

import("a.b.c", d, e, f)
local c = require "a.b.c"
local d, e, f = c.d, c.e, c.f

Realistic examples:

import("math", sin, cos, tan)
import("string", sub, gsub, find, gfind)

Yes, the standard parser could do this, too. It would be a bit
unorthogonal, but tremendously useful. Would've saved me a lot
of typing (and typos).