Quirrel4.41.0

types.Instance.rawset

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

Replaces the value stored at an existing field of this instance, without consulting a _newslot metamethod.

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 field name to update
valueanythe value to store there

Return value

The instance itself, so calls can be chained.

Errors

Throws the index '<key>' (type='<type>') does not exist if key is not already a field of the instance. Unlike a table or an unlocked class, an instance can never gain a new slot this way - the language reference states instance members cannot be removed, and the same restriction covers adding one. Attempting to rawset a method name fails the same way: a method is not a per-instance value slot to overwrite. On a frozen reference, throws Cannot modify immutable object.

Notes

Use rawset when the instance's class defines a _newslot metamethod and the plain (non-raw) assignment should not trigger it.

Example

examples/types/instance/rawset.nut
class Foo { x = 1 }
let f = Foo()
let same = f.rawset("x", 42)
println("same == f =", same == f, "f.x =", f.x)
try {
  f.rawset("newkey", 1)
} catch (e) {
  println("f.rawset(\"newkey\", 1) throws:", e)
}
Output:
same == f = true f.x = 42
f.rawset("newkey", 1) throws: the index 'newkey' (type='string') does not exist

See also

rawgetReads the value stored at key on this instance, without consulting a _get metamethod.
swapSwaps the values stored at two of this instance's fields.
newmemberAdds a member to the class, optionally as a static one.
instanceclass index