Quirrel4.41.0

types.Class.__merge

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

Builds a new class out of the class it is called on plus every argument given, and returns it. The class it is called on is not changed.

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 merge in
...anymore tables, classes, or instances to merge in, in order repeats

Return value

A new class with no base. It receives, in order, every own member of the receiver, then of other, then of each further argument; a later source overwrites a same-named member from an earlier one.

Errors

Throws a parameter type error naming the argument position when a source is not a table, class, or instance. Takes at least one argument beyond the receiver; calling it with none throws wrong number of parameters passed to native closure '__merge' (1 passed, at least 2 required).

Notes

Class.__merge and table.__merge are the same native function; only the receiver's type differs. It reads each source with a plain (non-raw) iteration, so a source's own _nexti or _get do not run, but every member lands in the new class through direct slot creation, not through _newslot.

Example

examples/types/class/__merge.nut
class A { a = 1 }
class B { b = 2 }
let merged = A.__merge(B)
println("merged.a =", merged.a, "merged.b =", merged.b)
println("A.rawin(\"b\") =", A.rawin("b"))
Output:
merged.a = 1 merged.b = 2
A.rawin("b") = false

See also

__updateCopies every own member of each argument into the class it is called on, and returns that same class. Unlike __merge, nothing new is allocated.
newmemberAdds a member to the class, optionally as a static one.
__mergeReturns a new table built from this table and every argument, later arguments overriding earlier ones.
classclass index