Skip to content

Commit 96be310

Browse files
committed
table.clone
1 parent d7929e8 commit 96be310

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

docs/Runtime Environment/Table.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,31 @@ print(test[2]) --> 5
397397
print(test[3]) --> 6
398398
```
399399

400+
---
401+
### `table.clone`
402+
Creates a copy of a table, optionally cloning nested tables up to a specified depth.
403+
#### Parameters
404+
1. The table to clone.
405+
2. The depth to clone to. Defaults to 100.
406+
#### Returns
407+
A new table containing the cloned values.
408+
```pluto
409+
local t = {
410+
a = {
411+
b = {
412+
c = {},
413+
},
414+
},
415+
}
416+
417+
local t1 = t:clone(1)
418+
assert(t1.a == t.a) -- depth 1 does not copy nested tables
419+
420+
local t2 = t:clone(2)
421+
assert(t2.a ~= t.a) -- 'a' is cloned
422+
assert(t2.a.b == t.a.b) -- but 'b' is shared
423+
```
424+
400425
---
401426
### `table.create`
402427
Creates a new empty table, preallocating memory. Improves performance and memory usage when you know how many elements the table will have.

0 commit comments

Comments
 (0)