lua-users home
lua-l archive

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


On Tue, Apr 8, 2014 at 6:53 AM, Claire Lewis <claire_lewis@live.com.au> wrote:
This is the best that can be done currently:
 https://github.com/Pogs/lua-snippets/blob/master/table-import.lua

It's handicapped in that it of course can't make locals.

Not sure if I’d call that the ‘best’, but it’s certainly one solution.

I’d probably prefer to use something like :

   function import(name)
       local mod = require(name)
       return function(t)
           for k,v in pairs(t) do
               t[k] = mod[v]
           end
           return unpack(t)
       end
   end

Import locals:

   local C,V = import "lpeg" { "C", "V" }

Import globals:

   C,V = import "lpeg" { "C", "V" }

Note this modifies the table passed in, but could just as easily return a new table.

- Claire.

I mean to say, you can't import everything from 'string' as locals without explicitly listing them.

table.import() is going by the mindset that you can't create locals in the inner-most scope at runtime.  So instead I made it general to copy references from one table to another, so you can copy directly into _ENV if you want.

You can't write Lua to do: import 'string' and create locals for every function therein
You can't write a module to do that either.  This is why I thought it needed upstream help.