types.Class.rawset
classCreates or replaces a member of the class, and unlike newmember, wires key and value to the roles their names suggest.
... from the prose below, not from the signature.Parameters
key | any | the member name to create or replace |
value | any | the 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
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)
}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 manuallySee also
rawget | Reads the value stored at key in the class's own member table. |
newmember | Adds a member to the class, optionally as a static one. |
lock | Locks the class immediately, without creating an instance. |
is_frozen | Reports whether this reference to the class was frozen with freeze(). |
| class | class index |