Page 1 of 1

[0.16.19] Minor Bug in lualib util.merge

Posted: Fri Jan 26, 2018 2:25 pm
by blueblue
The current merge function has inconsistent copying behavior for table members, it will sometimes deepcopy and at other times reference-copy. I am using the function to copy prototypes and make adjustments, and my modifications were sometimes affecting the original prototypes too.

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}
-- Suggested addition start
      elseif type(v) == "table" then
        ret[k] = table.deepcopy(v)
-- Suggested addition end
      else
        ret[k] = v
      end
    end
  end
  return ret
end

Re: [0.16.19] Minor Bug in lualib util.merge

Posted: Mon Jan 29, 2018 4:18 pm
by posila
I though about it and concluded you are right. Implementation you proposed is more proper.

Thanks for the report.
Changed for 0.16.21