Quirrel4.41.0

types.Table.clear

Method on every table
clear(): any

Removes every key/value pair from the table, in place.

examples/types/table/clear-basic.nut
let squad = {healer = 1, tank = 1}
squad.clear()
println("squad.len() =", squad.len())
Output:
squad.len() = 0

Return value

The table itself, now empty.

Errors

Throws Cannot modify immutable object when the table was frozen with freeze().

Notes

Takes no arguments. Existing references to the table see it become empty too, since clear mutates the same object rather than building a new one - unlike constructor(), which returns an unrelated new table.

Example

examples/types/table/clear.nut
let t = {a=1, b=2}
let alias = t          // same table, not a copy
let r = t.clear()
println("t.len() =", t.len())
println("r == t:", r == t)          // clear returns the table itself
println("alias.len() =", alias.len())     // the alias sees the same empty table

let frozen = freeze({a=1})
try {
  frozen.clear()
} catch (e) {
  println("frozen.clear() throws:", e)
}
Output:
t.len() = 0
r == t: true
alias.len() = 0
frozen.clear() throws: Cannot modify immutable object

See also

rawdeleteRemoves the slot at key, without calling a _delslot metamethod.
replace_withReplaces the table's entire contents with a copy of other's slots, in place.
is_frozenReports whether this reference to the table was frozen with freeze().
tableclass index