[16.16]util.merge attempt to call global merge (a nil value)
Posted: Thu Jan 18, 2018 8:48 pm
In util.lua there is a function called merge, it should merge two or more tables together. It works for the most part, except when having nested tables that need merging.
The recursive call tries to call merge without util:
It should be:
Code: Select all
/c game.print(serpent.block(util.merge(
{
{
a = { aa = 5 }
},
{
a = { ab = 5 }
}
})));
-- expected: { a = { ab = 5 }}
-- actually: Cannot execute command. Error: data/core/lualib/util.lua:206: attempt to call global 'merge' (a nil value)
Code: Select all
function util.merge(tables)
local ret = {}
for i, tab in ipairs(tables) do
for k, v in pairs(tab) do
if (type(v) == "table") and (type(ret[k] or false) == "table") then
ret[k] = merge{ret[k], v} --error message points to here
else
ret[k] = v
end
end
end
return ret
end
Code: Select all
function util.merge(tables)
local ret = {}
for i, tab in ipairs(tables) do
for k, v in pairs(tab) do
if (type(v) == "table") and (type(ret[k] or false) == "table") then
ret[k] = util.merge{ret[k], v}
else
ret[k] = v
end
end
end
return ret
end