Deep Copy and Shallow Copy
Implement recursive deep copy and understand reference semantics.
Deep Copy and Shallow Copy is a free Lua Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Reference Semantics in Lua
In Lua, assigning a table to another variable copies the reference, not the data. Both variables point to the same table.
local a = {1, 2, 3}
local b = a
b[1] = 99
print(a[1]) -- 99, a is affected!Shallow Copy
A shallow copy creates a new table with the same top-level keys and values. Nested tables are still shared.
local function shallowCopy(t)
local copy = {}
for k, v in pairs(t) do copy[k] = v end
return copy
endShallow Copy Limitation
Modifying a nested table in a shallow copy affects the original.
local orig = {data = {1, 2, 3}}
local sc = shallowCopy(orig)
sc.data[1] = 99
print(orig.data[1]) -- 99 (shared!)Deep Copy via Recursion
A deep copy recursively duplicates every nested table, breaking all shared references.
local function deepCopy(orig)
local copy
if type(orig) == "table" then
copy = {}
for k, v in pairs(orig) do
copy[deepCopy(k)] = deepCopy(v)
end
setmetatable(copy, getmetatable(orig))
else
copy = orig
end
return copy
endDeep Copy in Action
After a deep copy, modifying nested tables in the copy leaves the original untouched.
local orig = {data = {1, 2, 3}}
local dc = deepCopy(orig)
dc.data[1] = 99
print(orig.data[1]) -- 1 (independent!)Handling Cycles
Naive deep copy breaks on cyclic tables. Track visited tables in a memo table to handle cycles.
local function deepCopyMemo(orig, memo)
memo = memo or {}
if type(orig) ~= "table" then return orig end
if memo[orig] then return memo[orig] end
local copy = {}
memo[orig] = copy
for k, v in pairs(orig) do
copy[deepCopyMemo(k, memo)] = deepCopyMemo(v, memo)
end
setmetatable(copy, getmetatable(orig))
return copy
endCopying Metatables
Deep copy should also transfer the metatable with setmetatable(copy, getmetatable(orig)) to preserve OOP behavior.
Shallow Copy with table.move
For sequential arrays a fast shallow clone uses table.move.
local function arrayCopy(t)
return table.move(t, 1, #t, 1, {})
endWhen to Use Each
- Shallow: simple flat tables, performance-critical cloning.
- Deep: nested structures, serialization, immutable snapshots.
Function Values
Functions are never copied — both shallow and deep copy share the same function reference. This is usually the desired behavior.
Performance Considerations
Deep copy is O(n) in the number of nodes. For very large trees, consider structural sharing or copy-on-write patterns instead.
Copy Type Question
After a shallow copy, what happens when you modify a nested table in the copy?
Recap: Deep vs Shallow Copy
Shallow copy duplicates top-level keys only; nested tables are shared. Deep copy recursively duplicates everything. Handle cycles with a memo table to prevent infinite recursion.
Frequently asked questions
Is the “Deep Copy and Shallow Copy” lesson free?
Yes — the full text of “Deep Copy and Shallow Copy” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Deep Copy and Shallow Copy”?
Implement recursive deep copy and understand reference semantics. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Deep Copy and Shallow Copy” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- table.sort with Comparators
- table.concat and table.move
- Deep Copy and Shallow Copy
- Transforming Tables: map, filter, reduce