lua-users home
lua-l archive

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


On Apr 16, 2013, at 8:14 PM, Dimiter 'malkia' Stanev <malkia@gmail.com> wrote:

> I only wish there was easier way to do that in lua/luajit, a pattern along the line of "local something = something" to be just "import something" or "use something" or something like it.

Here is something, something then :P

local function import( aName, ... )
  local aModule = require( aName )
  local aFunction = debug.getinfo( 2, 'f' ).func
  local _, anEnvironement = debug.getupvalue( aFunction, 1 )

  for anIndex = 1, select( '#', ... ) do
    local aName = select( anIndex, ... )
    anEnvironement[ aName ] = aModule[ aName ]
  end
end

import( 'math', 'min', 'max' )

print( min )
print( max )
print( math )


But really… why bother?