Quirrel4.41.0

types.Instance.rawget

Method on every instance
pure instance.rawget(key): any

Reads the value stored at key on this instance, without consulting a _get 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 or method name to look up

Return value

The field's current value, or the method closure for key, read directly from the instance's class.

Errors

Throws the index doesn't exist when key is neither an own field nor a method of the instance's class; check with rawin first to avoid the throw.

Notes

Unlike a table's delegate, an instance's methods are not something rawget skips: they come from the class's own member layout, not from a _get fallback, so rawget sees them exactly as plain indexing would. What rawget does skip is a _get metamethod the class defines for keys that are not real members.

That skip only applies to the key argument, not to resolving the name rawget itself: a _get that returns a value for every key, instead of throw null for the ones it does not recognize, shadows someInstance.rawget (and every other built-in type-method) before the call ever happens, because plain . access tries _get before falling back to the type-methods table. someInstance.$rawget(key) bypasses _get for the method name and always reaches this method; see the .$ type-method operator. A well-behaved _get should throw null for keys it does not handle.

Example

examples/types/instance/rawget.nut
class Foo {
  x = 1
  function bar() { return 2 }
}
let f = Foo()
println("f.rawget(\"x\") =", f.rawget("x"))
println("typeof f.rawget(\"bar\") == \"function\" =", typeof f.rawget("bar") == "function")
try {
  f.rawget("missing")
} catch (e) {
  println("f.rawget(\"missing\") throws:", e)
}
Output:
f.rawget("x") = 1
typeof f.rawget("bar") == "function" = true
f.rawget("missing") throws: the index doesn't exist

See also

rawinTests whether key names a field or method reachable on this instance.
rawsetReplaces the value stored at an existing field of this instance, without consulting a _newslot metamethod.
rawgetReads the value stored at key in the class's own member table.
instanceclass index