lua-users home
lua-l archive

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


On Fri, Dec 10, 2010 at 03:41:14AM +0200, David Manura wrote:
> If you like, I suggest this:
> 
>   -- math_ext.lua
>   local M = {}
>   for k,v in pairs(math) do M[k] = v end
>   function M.floor(n, p) ..... end
>   return M
> 
>   -- test.lua
>   local math = require "math_ext"
>   print(math.floor(math.pi, 2))
> 

This problem is not confined to math_ext.lua, so why not a more 
general solution: define functions M.foo etc inside 
    local M ... return M
but don't otherwise populate M yet.  

-- myutils.lua
function import(from,to,items)
-- import(from,to) copies everything; can be used as shallow table copy
-- import(from) copies to current environment
-- import("name") copies from require"name"
    to = to or _ENV or getfenv()  -- specified or Lua 5.2 or Lua 5.1 
    if type(from)=='string' then from=require(from) end
    if items 
        then for i=1,#items do to[items[i]] = from[items[i]] end 
        else for k,v in pairs(from) do to[k]=v end
        end
    return to
    end

-- test.lua
require"myutils"    -- I do this habitually anyway 
import("math_ext",math) 

Dirk