Quirrel4.41.0

types.Table.rawget

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

Returns the value stored under key, without going through a 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 key to look up

Return value

The value stored under key.

Errors

Throws the index doesn't exist when key is not in the table.

Notes

Takes exactly one argument - t.rawget() and t.rawget("a", "b") both throw a wrong-number-of-parameters error rather than reaching this code.

Plain indexing, t[key], and rawget both throw on a missing key for an ordinary table (with different wording - t[key] throws the index 'key' (type='...') does not exist), because a table's own _get metamethod can only come from a delegate, and a delegate can only be attached from C++ (see getfuncinfos). The distinction between raw and plain access matters far more for class/ instance, where _get is reachable from script and can turn a "missing" key into a computed value.

Example

examples/types/table/rawget.nut
let t = {a=1, b=2}
println("t.rawget(\"a\") =", t.rawget("a"))
println("t[\"a\"] =", t["a"])          // same result: a table has no reachable _get here

try {
  t.rawget("missing")
} catch (e) {
  println("t.rawget(\"missing\") throws:", e)
}

try {
  t.missing               // plain indexing also throws, with different wording
} catch (e) {
  println("t.missing throws:", e)
}
Output:
t.rawget("a") = 1
t["a"] = 1
t.rawget("missing") throws: the index doesn't exist
t.missing throws: the index 'missing' (type='string') does not exist

See also

rawsetSets the slot at key to value, creating the slot if it does not exist yet.
rawinReports whether key is a slot in the table, without calling a metamethod.
hasindexReports whether key is a slot in the table.
tableclass index