[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: modules, require, magic
- From: Patrick Donnelly <batrick@...>
- Date: Wed, 19 Oct 2011 15:49:05 -0400
On Wed, Oct 19, 2011 at 1:26 PM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>>
>> On Oct 19, 2011, at 5:32 PM, Roberto Ierusalimschy wrote:
>>
>> > If you really want 'module', wouldn't be simpler to use
>> >
>> > _ENV = module(...)
>> >
>> > with a simplified version of 'module'?
>>
>> By simplified, do you mean a 5.2 version not doing any of the setfenv() mambo jambo of the 5.1 implementation, but otherwise equivalent in functionality?
>>
>> With the environment switching as an explicit _ENV assignment?
>
> For instance (also without the global setting). Something like this:
>
> function module (name, ...)
> local env = package.loaded[name]
> if env == nil then
> env = {}
> package.loaded[name] = env
> end
> env.NAME = name
> env.M = env
> for _, f in ipairs{...} do
> f(env)
> end
> return env
> end
[Assuming the above function were put in Lua 5.2,] package.seeall has
questionable usefulness even-more-so now that modules are not put in
the global environment. I would also second the syntactic sugar
addition of something like:
local abs, floor in require "math"
or similar. That way we can nicely document dependencies without
taking dozens of lines of code to do it.
--
- Patrick Donnelly
- 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, Roberto Ierusalimschy
- Re: modules, require, magic, Petite Abeille
- Re: modules, require, magic, Roberto Ierusalimschy