lua-users home
lua-l archive

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


Justin Cormack <justin@specialbusservice.com> 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 'foo.bar'
>>
>> and have a table named 'bar' in the local environment (not global).
>>
>
> No.
>
> local bar = require "foo.bar"
>
> 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 "foo.bar"' 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("foo.bar", _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
table:

import { _ENV,
    "foo.bar",    -- will be stored in _ENV['bar']
    "os",
    "math",
}

... 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 = "foo.bar",
    math_iz_awezome = "math",
    "os",
}

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


James