Skip to main content

array

Macro array 

macro_rules! array {
    ($($elements:expr_2021),* $(,)?) => { ... };
    (= $($elements:expr_2021),* $(,)?) => { ... };
}
Expand description

Constructs Array literals, similar to Rust’s standard vec! macro.

§Type inference

By default, array! uses AsArg to push elements. This works when the array’s element type T is already determined from context – a type annotation, function parameter, etc. This supports all element types including Gd<T> and Variant.

The = prefix (array![= ...]) switches to AsDirectElement, enabling unambiguous type inference from the elements themselves – no external type context needed. Covers only common types and provides only conversions that are opinionated but unambiguous. For example, array![= "hello"] will infer as Array<GString>, never Array<StringName> or Array<Variant>.

§Examples

// Type annotation provides context; no prefix needed.
// Multiple assignments possible.
let ints: Array<i8>      = array![3, 1, 4];
let ints: Array<i64>     = array![3, 1, 4];
let ints: Array<Variant> = array![3, 1, 4];

let strs: Array<GString>    = array!["a", "b"];
let strs: Array<StringName> = array!["a", "b"];
let strs: Array<Variant>    = array!["a", "b"];

// No type context, so use `=` for unambiguous inference.
let ints = array![= 3, 1, 4];  // Array<i32>.
let strs = array![= "a", "b"]; // Array<GString>.

§See also

To create an Array of variants, see the varray! macro.

For dictionaries, a similar macro dict! exists.