types.Table.__merge
Returns a new table built from this table and every argument, later arguments overriding earlier ones.
... from the prose below, not from the signature.Parameters
other | table|instance|class | a 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
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)r == t: false
r.fizz = 1 r.buzz = 2
"buzz" in t: false
frozen.__merge({b=2}).b = 2See also
__update | Copies every slot from each argument into the table, in place, later arguments overriding earlier ones. |
replace_with | Replaces the table's entire contents with a copy of other's slots, in place. |
clone | Returns a shallow copy of the table. |
| table | class index |