Quirrel4.41.0

types.Table.__merge

Method on every table
pure (table|class).__merge(other: table|instance|class, ...): any

Returns a new table built from this table and every argument, later arguments overriding earlier ones.

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

othertable|instance|classa table, class or instance to copy slots from; repeatable, so more than one may be given
...any repeats

Return value

A new table, seeded with this table's own slots first, then each other in order, each overwriting slots the previous ones set. Neither this table nor any other is modified.

Errors

Throws a type error when an argument is not a table, class or instance, but the wording depends on which one: a bad first other throws parameter 1 of '__merge' has an invalid type '...' ; expected: 'table|class|instance', while a bad second or later argument throws parameter N of '<unknown>' has an invalid type '...' ; expected: 'table|class|instance', with N counting the table itself as parameter 1. Only the first argument goes through the VM's own automatic type check, which knows the function's name; the rest are checked by hand inside __merge, which does not pass a name along.

Notes

Takes 1 or more arguments - this method is variadic, unlike reduce or findvalue on this page, where a negative arity in the binding does not mean open-ended. t.__merge() with no arguments throws a wrong-number-of-parameters error.

Unlike __update, __merge never touches t; it builds and returns a separate table instead. Being read-only on t, it still works on a table frozen with freeze() - there is nothing here for the immutable check to reject.

Example

examples/types/table/__merge.nut
let t = {fizz=1}
let r = t.__merge({buzz=2})
println("r == t:", r == t)           // a new table, not t itself
println("r.fizz =", r.fizz, "r.buzz =", r.buzz)
println("\"buzz\" in t:", "buzz" in t)      // t itself never gained the new key

// works on a frozen table too: it only reads t, never writes it
let frozen = freeze({a=1})
println("frozen.__merge({b=2}).b =", frozen.__merge({b=2}).b)
Output:
r == t: false
r.fizz = 1 r.buzz = 2
"buzz" in t: false
frozen.__merge({b=2}).b = 2

See also

__updateCopies every slot from each argument into the table, in place, later arguments overriding earlier ones.
replace_withReplaces the table's entire contents with a copy of other's slots, in place.
cloneReturns a shallow copy of the table.
tableclass index