Androider
Androider

Reputation: 21335

How to call lua functions from external library from multiple sources?

I am new to lua and am trying to do the following:

lets say there is an external module mycalculator.lua and it has a function myadd(a,b).

Now I want to basically have two more files A.lua and B.lua that uses mycalculator. They both use myadd(a,b) from mycalculator but have their own functions mycalcA and mycalcB which I want to be able to call in a third function in C.lua. I need to understand how I must load these and in what sequence. Are the requirement dependencies in C.lua correct? Can this be done or is it circular? Also can I overload mycalc(a,b) so it is same function name in both A and B?

-- A.lua
require 'mycalculator'

a=1;
b=2;
n=20;

function mycalcA(a,b)
     return myadd(a,b)+20;
end
function  A()
  local A = {}
     A.mycalcA = mycalcA
  return A
end

-- B.lua

require 'mycalculator'

a=1;
b=2;
X=50;

function mycalcB(a,b)
     return myadd(a,b)+50;
end
function  B()
  local B = {}
     B.mycalcB = mycalcB
  return B
end

-- C.lua

require 'A'
require 'B'

print(mycalcB(1,2))
print(mycalcA(1,2))

Upvotes: 1

Views: 1912

Answers (2)

greatwolf
greatwolf

Reputation: 20878

Are the requirement dependencies in C.lua correct? Can this be done or is it circular?

Just to expand on John's answer a little. When you require a module in lua, it first checks if that module's already loaded. Circular dependency isn't an issue here because if module 'A' loads mycalculator first and module 'B' requires it after, the lua VM won't reload mycalculator again.

FYI, lua keeps track of what modules loaded through a table package.loaded. When a new module's loaded that table gets updated with a new entry with the module as the entry name. Modules that aren't loaded yet won't have an entry in package.loaded and so its value will be nil.

One other subtle thing in your code that John correct:

-- A.lua
require 'mycalculator'

a=1;
b=2;
n=20;
-- ...

-- B.lua
require 'mycalculator'

a=1;
b=2;
x=50;
-- ...

Those variables are global by default. When you require module 'A' and 'B' those variables will get dumped into your 'C' module's global namespace which may not be what you want. To better compartmentalization this and keep it at file scope just prefix those variables with 'local' as shown in John's example.

Upvotes: 2

John Zwinck
John Zwinck

Reputation: 249592

  • Are the requirement dependencies in C.lua correct? Can this be done or is it circular?
    • What you have looks fine to me.
  • Can I overload mycalc(a,b) so it is same function name in both A and B?
    • I don't think that would make sense without another change, which would be to scope the definitions in A and B and return a table from each. That would look like this:

B.lua (and A.lua similarly):

require 'mycalculator'

local a=1;
local b=2;
local X=50;

local function mycalcB(a,b)
     return myadd(a,b)+50;
end

B = {
    mycalc = mycalcB
}
return B

C.lua:

require 'A'
require 'B'

print(B.mycalc(1,2))
print(A.mycalc(1,2))

Upvotes: 4

Related Questions