ellipsisSpread Syntax

Use spread syntax in literals and function calls, plus shorthand struct keys.

circle-info

Since BoxLang 1.12.x

Spread syntax uses ... to expand arrays and structs into function calls and literals.

BoxLang also supports struct shorthand keys, which pair naturally with spread-heavy code.

📋 Table of Contents

🚀 Function call spread

Use spread in a function call to expand an array as positional arguments or a struct as named arguments.

Spread an array into positional arguments

function add( a, b, c ) {
    return a + b + c
}

args = [ 1, 2, 3 ]
result = add( ...args ) // 6

Spread a struct into named arguments

Inline literals

Works with more than plain function calls

Spread also works with:

  • built-in functions

  • member methods

  • closures

  • lambdas

  • returned function expressions

circle-info

Function-call spread uses the same behavior as argumentCollection. Arrays expand positionally. Structs expand by argument name.

📦 Array literal spread

Use spread inside an array literal to inline another array.

This works with inline values too.

🧱 Struct literal spread

Use spread inside struct literals to merge keys from one or more sources.

Unordered struct literals

Ordered struct literals

Ordered struct literals also support spread.

Spreading arrays into structs

When you spread an array into a struct literal, BoxLang uses 1-based numeric keys.

That behavior also works with ordered structs.

✨ Struct shorthand keys

Struct literals support JavaScript-style shorthand keys.

That is equivalent to this:

Shorthand preserves the identifier name exactly.

You can mix shorthand, explicit pairs, and spread.

🧪 Spread-only bracket literals

Bracket literals that contain only spread entries are resolved at runtime.

If the spread sources are arrays

The result is an array.

If the spread sources are structs

The result is an ordered struct.

circle-exclamation

🔁 Merge precedence

Spread follows declaration order. Later entries win.

The same rule applies to ordered structs.

⚠️ Common errors

Non-spreadable values

You can only spread values that BoxLang can treat as arrays or structs for the target literal.

Null spread values

Spreading null into a literal is invalid.

Mixed spread-only bracket sources

A spread-only bracket literal must resolve consistently. Do not mix array and struct sources.

✅ Summary

Use spread syntax when you want to:

  • expand arguments into a function call

  • merge arrays into arrays

  • merge structs into structs

  • build concise literals

  • combine shorthand keys with explicit overrides

Use spread-only bracket literals carefully. Use explicit keyed members when you want clearer intent.

Last updated

Was this helpful?