scheurneus
scheurneus

Reputation: 155

Lua specifying library

Within my Lua scripts, I have multiple libraries using the same 'structure'. For example, I have a.lua what contains 'require('b')' and require('c'). Both b.lua and c.lua have got an info function. b.lua let it print "b" and c.lua let it print "c". Now I want to be able to specify what one I use in a.lua.

Upvotes: 2

Views: 182

Answers (1)

greatwolf
greatwolf

Reputation: 20878

Put your 'b' and 'c' modules into different namespaces by using tables and then explicitly qualify which one to use from a.lua. For example:

-- b.lua
local function info()
  print "b"
end

return { info = info }

-- c.lua
-- another style
local M = {}
function M.info()
  print "c"
end

return M

-- a.lua
b = require 'b'
c = require 'c'

b.info()  -- prints "b"
c.info()  -- prints "c"

local info = b.info -- ok you really want 'b'
info()    -- prints "b"

Upvotes: 4

Related Questions