types.Instance.swap
Method on every
instanceSwaps 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
key1 | any | the first field name |
key2 | any | the 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
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 existSee also
rawset | Replaces the value stored at an existing field of this instance, without consulting a _newslot metamethod. |
rawget | Reads the value stored at key on this instance, without consulting a _get metamethod. |
swap | Exchanges the values stored at key1 and key2. |
| instance | class index |