lua-users home
lua-l archive

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

Justin Cormack <> wrote:

>> 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:
>> import ''
>> and have a table named 'bar' in the local environment (not global).
> No.
> local bar = require ""
> Is the shortest version. Without macros there is no other option as locals
> are syntactic (for good reasons). But without ability to rename on import
> you may as well use globals!

I realize that 'bar' can't actually be a local variable created by the
'import ""' statement.  There is no way to programmatically
create locals unless we use a macro package.

But is there some way (perhaps by using the debug library) to insert
'bar' into the current _ENV without having to pass that in to the
import function every time?

If I have to do something like this:

import("", _ENV)

that isn't any better (shorter) than what I do now.  Or maybe I should
just create an import function that takes a list of packages in a

import { _ENV,
    "",    -- will be stored in _ENV['bar']

... and give up the idea of having separate import or require
declarations, one per line.  I suppose you could support package
naming by specifying string keys:

import { _ENV,
    baz = "",
    math_iz_awezome = "math",

Hmmmm.... maybe I should write that up.