types.Table.clone
Method on every
tableclone(): any
Returns a shallow copy of the table.
Return value
A new table with the same keys and values. Nested tables and arrays are shared with the original, not copied again.
Notes
Takes no arguments. The clone is always a plain mutable table, even when the original was frozen with freeze(): cloning drops the immutable flag rather than copying it.
clone is a keyword as well as a method name, so by default t.clone() and t.$clone() do not parse: the compiler reads clone after a dot as the keyword. Call it as t["clone"](), or write clone t instead. The clone operator on a table runs this same method. With #forbid-clone-operator the word is an ordinary identifier and t.$clone() compiles.
Example
let t = {a=1, nested={x=1}}
let c = clone t // the clone operator runs this method
println("c == t:", c == t) // a different table
println("c.a =", c.a)
c.a = 2
println("t.a =", t.a) // unaffected: shallow copy at the top level
println("c.nested == t.nested:", c.nested == t.nested) // but nested tables are shared, not copied
let frozen = freeze({a=1})
println("frozen.is_frozen() =", frozen.is_frozen())
println("(clone frozen).is_frozen() =", (clone frozen).is_frozen()) // the clone is always mutableOutput:
c == t: false
c.a = 1
t.a = 1
c.nested == t.nested: true
frozen.is_frozen() = true
(clone frozen).is_frozen() = falseSee also
is_frozen | Reports whether this reference to the table was frozen with freeze(). |
weakref | Returns a weak reference to the table. |
replace_with | Replaces the table's entire contents with a copy of other's slots, in place. |
| table | class index |