Quirrel4.53.0

types.Table.rawset

Method on every table
table.rawset(key, value): any

Sets the slot at key to value, creating the slot if it does not exist yet.

The binding carries no declaration string, so the VM cannot report parameter names. The names above are from this page; the types and attributes still come from the VM. The VM also cannot tell an optional parameter from a variadic tail here, so read the brackets and any trailing ... from the prose below, not from the signature.

Parameters

keyanythe key to write
valueanythe value to store there

Return value

The table itself.

Errors

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

Notes

Takes exactly two arguments; t.rawset("k") and t.rawset("k", 1, 2) both throw a wrong-number-of-parameters error.

Unlike the assignment operator t.k = value, which requires the slot to already exist (use t.k <- value to create one), rawset always creates a missing slot - there is no separate "raw newslot" method, rawset covers both cases.

Example

examples/types/table/rawset.nut
let t = {a=1}
let r = t.rawset("b", 2)  // creates a new slot, unlike plain '=' below
println("r == t:", r == t)
println("t.b =", t.b)

try {
  t.c = 3                   // '=' requires the slot to already exist
} catch (e) {
  println("t.c = 3 throws:", e)
}

let frozen = freeze({a=1})
try {
  frozen.rawset("a", 2)
} catch (e) {
  println("frozen.rawset(\"a\", 2) throws:", e)
}
Output:
r == t: true
t.b = 2
t.c = 3 throws: the index 'c' (type='string') does not exist
frozen.rawset("a", 2) throws: Cannot modify immutable object

See also

rawgetReturns the value stored under key.
rawdeleteRemoves the slot at key.
rawinReports whether key is a slot in the table.
tableclass index