[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: modules, require, magic
- From: Tony Finch <dot@...>
- Date: Wed, 19 Oct 2011 17:52:23 +0100
Hisham <hisham.hm@gmail.com> wrote:
>
> But having to write "module.bar" (or "M.bar", or "_M.bar"...) in every
> use of module functions within the module is cumbersome.
It is wrong to use _M because that abuses a reserved namespace.
> None of them are as clean as the programming style with module().
Muddling up the global and module namespaces is not clean.
(Argumentative point, sorry. We've had too much of that in this thread.)
It's easy to reverse the priority of the global and module namespaces,
if you don't like repeated "module." prefixes:
local G, _ENV = _ENV, module
I think the naming problem is the main reason people like module(), but it
provides nice names by breaking abstractions which the main reason people
dislike it. But there is more to the naming problem than just module().
A few times in this discussion people have mentioned the import problem,
e.g. from strict.lua,
local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget
where the commonly suggested improvement is
local error, rawset, rawget in _ENV
local getinfo in debug
There is a similar export problem, e.g. at the end of a module,
return { foo = foo, bar = bar }
When importing we are projecting from a table into a set of local
variables and when exporting we are injecting a set of local variables
into a table. In both cases we currently have to repeat the names to
get quoted and unquoted versions. (Note that syntactic sugar hides the
quotes.)
It is hard to come up with a table injection syntax that is as neat as the
projection syntax. Here's one that I don't much like. Any better
suggestions?
return { in foo, bar }
The require function also has this double use+mention problem:
local socket = require "socket"
I wonder if local vars in tbl could be generalized to allow a function on
the right hand side, e.g. the following, though you would need a
multi-valued version of require with a better name :-)
local socket in require
I also wonder if there's a deeper pattern that could lead to a better
syntax for all three of these use+mention examples. Are there other cases
where use+mention causes tiresome code in Lua?
Tony.
--
f.anthony.n.finch <dot@dotat.at> http://dotat.at/
Biscay: Mainly north 4 or 5. Moderate or rough. Showers. Moderate or good,
occasionally poor at first in south.
- References:
- modules, require, magic, Eduardo Ochs
- Re: modules, require, magic, Javier Guerra Giraldez
- Re: modules, require, magic, Petite Abeille
- Re: modules, require, magic, Sam Roberts
- Re: modules, require, magic, David Manura
- Re: modules, require, magic, Hisham
- Re: modules, require, magic, Tony Finch
- Re: modules, require, magic, Hisham