Spread Syntax
Use spread syntax in literals and function calls, plus shorthand struct keys.
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.
๐ 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 ) // 6Spread a struct into named arguments
function greet( first, last ) {
return "Hello, " & first & " " & last
}
person = { first: "John", last: "Doe" }
result = greet( ...person ) // "Hello, John Doe"Inline literals
Works with more than plain function calls
Spread also works with:
built-in functions
member methods
closures
lambdas
returned function expressions
๐ฆ 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.
๐ 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?
