Quirrel4.41.0

types.Array.extend

Method on every array
array.extend(other: array, ...): any

Appends every element of other to this array.

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

otherarrayan array whose elements are copied in; give more than one to chain several in one call
...any repeats

Return value

This array.

Errors

Throws only arrays can be used to extend array when a second or later argument is not an array; a non-array first argument is instead rejected by the VM itself with its own wrong-type message, since that one argument is covered by the binding's type mask.

Notes

This binding's type mask covers only the first argument, so the VM's signature shows one parameter, but extend accepts any number of arrays and appends them in order: a.extend(b, c) is a.extend(b); a.extend(c) done in one call. Every argument is checked to be an array before any of them are appended.

Example

examples/types/array/extend.nut
let a = [1];
a.extend([2, 3], [4]);   // more than one array in a single call
println("a =", ", ".join(a.map(@(v) v.tostring())));

try { [1].extend(2) } catch (e) { println("[1].extend(2) throws:", e); }        // first argument: VM type check
try { [1].extend([2], 3) } catch (e) { println("[1].extend([2], 3) throws:", e); }    // later argument: extend's own check
Output:
a = 1, 2, 3, 4
[1].extend(2) throws: parameter 1 of 'extend' has an invalid type 'integer' ; expected: 'array'
[1].extend([2], 3) throws: only arrays can be used to extend array

See also

appendAdds one or more values to the end of the array.
replace_withOverwrites this array's elements with other's.
arrayclass index