Quirrel4.41.0

types.Instance.swap

Method on every instance
instance.swap(key1, key2): any

Swaps the values stored at two of this instance's fields.

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

key1anythe first field name
key2anythe second field name

Return value

The instance itself, so calls can be chained.

Errors

Throws the index doesn't exist if either key is not an existing field of the instance. On a frozen reference, throws Cannot modify immutable object.

Notes

Instance.swap, types.Table.swap, and types.Array.swap are the same native function; only the receiver's type differs, and for an array the two arguments are numeric indices rather than key names.

Example

examples/types/instance/swap.nut
class Pair { a = 1 b = 2 }
let p = Pair()
p.swap("a", "b")
println("p.a =", p.a, "p.b =", p.b)
try {
  p.swap("a", "missing")
} catch (e) {
  println("p.swap(\"a\", \"missing\") throws:", e)
}
Output:
p.a = 2 p.b = 1
p.swap("a", "missing") throws: the index doesn't exist

See also

rawsetReplaces the value stored at an existing field of this instance, without consulting a _newslot metamethod.
rawgetReads the value stored at key on this instance, without consulting a _get metamethod.
swapExchanges the values stored at key1 and key2.
instanceclass index