Quirrel4.41.0

types.Table.constructor

Method on every table
constructor(): any

Returns a new, empty table.

Return value

A fresh table with no keys, unrelated to the table constructor was called on.

Notes

Takes no arguments. Unlike a class's constructor, calling this one on an existing table t does not rebuild t in place: t.constructor() leaves t untouched and returns a different, empty table. Use clear to empty t itself, or the {} literal to make a new empty table directly; this method exists mainly so the table delegate has a constructor slot, matching every other type.

Example

examples/types/table/constructor.nut
let t = {a=1, b=2}
let r = t.constructor()
println("r.len() =", r.len())       // the result is a brand-new, empty table
println("t.len() =", t.len())       // t itself is untouched
println("r == t:", r == t)

// takes no arguments at all
try {
  t.constructor(1)
} catch (e) {
  println("t.constructor(1) throws:", e)
}
Output:
r.len() = 0
t.len() = 2
r == t: false
t.constructor(1) throws: wrong number of parameters passed to native closure 'constructor' (2 passed, 1 required)

See also

cloneReturns a shallow copy of the table.
clearRemoves every key/value pair from the table, in place.
tableclass index