← Back to Gists

Table shallow copy

📝 Lua
hendersonanne654
hendersonanne654 · 22d ago
Creates a new table with the same top-level key-value pairs as the original without duplicating nested tables.
Lua
function shallowcopy(t) local copy = {} for k, v in pairs(t) do copy[k] = v end return copy
end

Comments

0
scottm scottm 5d ago
so does that mean you're doing a shallow copy but skipping nested recursion? what happens if the original has metatables - are they preserved or transferred to the new table?