Atom

copy()

Change a shallow copy

Copy an array before changing one top-level slot.

Atom
original = [4]
duplicate = copy(original)
duplicate[0] = 9
print(original[0])
Output
4

The new array has its own slots.

Copy nested data independently

Pass true to recursively copy the nested collections.

Atom
original = {player = {score = 4}}
duplicate = copy(original, true)
duplicate.player.score = 9
print(original.player.score)
Output
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

Atom
a = {}
a.link = a
b = copy(a, true)
assert(b != a and b.link == b)