Quirrel4.41.0

types.Array.constructor

Method on every array
constructor(size: number, [default_value], ...): any

Creates an array of size elements, each set to default_value.

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

sizenumbernumber of elements
default_valueanyvalue stored in every element; null when left out optional
...any repeats

Return value

The new array.

Errors

Throws array size must be non-negative when size is negative.

Notes

This is the same native function as the global array, reached here because calling a built-in type's class object runs its constructor: types.Array(3, 0) and array(3, 0) do the same thing. It ignores any existing array it might be called on; a.constructor(2) builds and returns a fresh array instead of resetting a.

default_value is stored as is, not cloned per slot, so a table or array given as the fill value ends up shared by every element; see array for that in detail.

Only size is required; a third real argument is accepted but silently ignored, the same way resize ignores one.

Example

examples/types/array/constructor.nut
let types = require("types");

let a = types.Array(3, 0);   // same constructor that backs the global array()
println("a.len() =", a.len(), "a[0] =", a[0]);

let b = types.Array(2);       // default_value left out: every element is null
println("type(b[0]) =", type(b[0]));

try { types.Array(-1) } catch (e) { println("types.Array(-1) throws:", e); }
Output:
a.len() = 3 a[0] = 0
type(b[0]) = null
types.Array(-1) throws: array size must be non-negative

See also

arrayCreates an array of size elements, each set to default_value.
resizeGrows or shrinks the array to size elements.
arrayclass index