copy()
Change a shallow copy
Copy an array before changing one top-level slot.
original = [4]
duplicate = copy(original)
duplicate[0] = 9
print(original[0])
4
The new array has its own slots.
Copy nested data independently
Pass true to recursively copy the nested collections.
original = {player = {score = 4}}
duplicate = copy(original, true)
duplicate.player.score = 9
print(original.player.score)
4
The nested player is copied too. A shallow copy would still share it.
Copies an ordinary collection or packed value.
Signature
copy(value[, deep])
Parameters
Any value, and optional boolean deep flag, default false.
Returns
A new collection or packed storage, or the original value for other types including modules and functions.
Behavior
Shallow ordinary copies retain nested references. Deep copies recurse through keys and values and preserve cycles and aliases. A packed copy owns its bytes.
Errors
Excessive nesting, invalid packed view or allocation failure.
Example
a = {}
a.link = a
b = copy(a, true)
assert(b != a and b.link == b)