[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua round-trip?
- From: Pierre-Yves Gérardy <pygy79@...>
- Date: Sun, 10 Nov 2013 23:56:34 +0100
Another option would be to use dependency injection. Rather than
returning a table, return a function:
-- file:data.lua
return function (functions)
return {
{
name='foo',
val=42,
onA = functions.onA1,
},
{
name='bar',
val=123,
onA = functions.onA2,
onB = functions.onB2,
},
}
end
in another file, you'll keep the functions in a corresponding table,
and pass it to the return value of
-- file:functions.lua
return {onA1 = function()end, onA2=function()end, ...}
then
-- file:main.lua
local funcs = require"functions"
local data1 = require"data"(funcs)
-- modify data.lua, remove data from package.loaded (or use dofile
instead of require, in whch case you don't need to clean up the package cache)
local data2 = dofile"data"(funcs)
Another approach (a bit cleaner if you don't use require) :
-- file:data-bis.lua
local functions = ... -- a chunk is actually a vararg function.
return {
{
name='foo',
val=42,
onA = onA1,
},
{
name='bar',
val=123,
onA = onA2,
onB = onB,
},
}
-- file:main-bis.lua
functions = require"functions"
data = loadfile"data-bis.lua"(functions)
-- etc...
Maybe this would fit your use case?
-- Pierre-Yves