[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Local as table
- From: James Graves <james.c.graves.jr@...>
- Date: Thu, 21 Feb 2013 11:50:13 -0600
steve donovan <email@example.com> wrote:
> On Thu, Feb 21, 2013 at 4:10 PM, Andrew Starks <firstname.lastname@example.org> wrote:
> > This all made me think that if Lua had a macro processor, turning a
> > table into pre-defined locals would be pretty trivial. Something like:
> There have been suggestions about e.g. 'import sin,cos from math' but
> the feeling is that this is just sugar, and sugar is bad for you ;)
> BTW, many of the 'nice-to-have' features that don't get into Lua have
> ended up in Moonscript, which has this very statement and feels good
> to use.
I'm currently liking the way Go  does imports, where the package
name is retained. For example, you may need the cosine function in
the math package. So you'll have an import statement:
And then you'll normally refer to the function as 'math.Cos' after
that in this file, though you have options to rename the package or
import everything from 'math' into the local namespace.
At any rate, the normal import block at the top of each file is short
and to the point. Also, when reading Go code, if you see 'foo.bar',
you can just look for 'foo' at the top of the file to see where it
came from. There is no mystery, and no disturbance to the global
namespace. Yes, it is a little verbose in the regular program text
(because you normally see 'math.Cos' instead of just 'Cos'), but I
think the reduction in ambiguity is worth it.
And this roughly corresponds to how I normally use modules with Lua.
local os = require 'os'
For various reasons I'm not likely to be switching to Moonscript in
the near future. :-/
So, my main question is: Is there any way to have some kind of import
statement (for regular Lua) that doesn't require passing in the
current _ENV value? Where I just have this:
and have a table named 'bar' in the local environment (not global).