Quirrel4.41.0

types.Class.rawset

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

Creates or replaces a member of the class, and unlike newmember, wires key and value to the roles their names suggest.

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 member name to create or replace
valueanythe value to store there

Return value

The class itself, so calls can be chained.

Errors

On a locked class, throws trying to modify a class that has already been instantiated, inherited or is locked manually for any plain-value key, not only a new one: locking blocks updating an existing field's default just as much as adding a new field. A closure or native closure value is exempt from that check and is always accepted (it becomes a method). On a frozen reference, throws Cannot modify immutable object - a different message from the one newmember raises for the same condition, because this check runs earlier, in sqbaselib.cpp, rather than in SQVM::NewSlot.

Notes

On an unlocked class, rawset on an existing key overwrites that field's default value for future instances; instances already created keep whatever value they copied at construction. This is the same rule the language reference states for ClassName.field <- value after declaration.

Example

examples/types/class/rawset.nut
class Foo { x = 1 }
let same = Foo.rawset("x", 99)
println("same == Foo =", same == Foo, "Foo.x =", Foo.x)
Foo.lock()
try {
  Foo.rawset("x", 5)
} catch (e) {
  println("Foo.rawset(\"x\", 5) throws:", e)
}
Output:
same == Foo = true Foo.x = 99
Foo.rawset("x", 5) throws: trying to modify a class that has already been instantiated, inherited or is locked manually

See also

rawgetReads the value stored at key in the class's own member table.
newmemberAdds a member to the class, optionally as a static one.
lockLocks the class immediately, without creating an instance.
is_frozenReports whether this reference to the class was frozen with freeze().
classclass index