Quirrel4.41.0

types.Table.hasindex

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

Reports whether key is a slot in the table.

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 test

Return value

true if key names a slot in the table, false otherwise.

Notes

Takes exactly one argument; t.hasindex() throws a wrong-number-of-parameters error.

Implemented the same way as rawin: both call sq_rawget directly, so neither ever consults a _get metamethod. The two exist as separate methods because the shared C++ function also backs class and instance, where hasindex additionally accepts those types; on a table the two methods behave identically.

Example

examples/types/table/hasindex.nut
let t = {a=1, b=null}
println("t.hasindex(\"a\") =", t.hasindex("a"))
println("t.hasindex(\"missing\") =", t.hasindex("missing"))
println("t.hasindex(\"b\") =", t.hasindex("b"))       // present with a null value: still true
println("t.hasindex(\"a\") == t.rawin(\"a\"):", t.hasindex("a") == t.rawin("a"))  // identical on a table
Output:
t.hasindex("a") = true
t.hasindex("missing") = false
t.hasindex("b") = true
t.hasindex("a") == t.rawin("a"): true

See also

hasvalueReports whether value is stored under any key in the table.
rawinReports whether key is a slot in the table, without calling a metamethod.
rawgetReturns the value stored under key, without going through a metamethod.
tableclass index