Destructuring
Unpack structs and arrays into variables with defaults, nesting, and rest captures.
Destructuring lets you unpack values from structs and arrays into variables in a single expression.
It works in declarations like var and in assignments.
๐งฉ Struct destructuring
Struct destructuring binds keys by name.
user = { name: "Luis", role: "admin" }
var { name, role } = user
println( name )
println( role )You can also rename keys while destructuring.
config = { host: "localhost", port: 5432 }
var { host, port: dbPort } = config
println( host )
println( dbPort )While renaming a struct key, you can also place it into a different scope. (You may not use the var, final, or static keywords in these cases.)
config = { host: "localhost", port: 5432 }
({ host: variables.host, port: request.dbPort } = config)
println( variables.host )
println( request.dbPort )๐ Array destructuring
Array destructuring binds values by position.
This also works with assignments.
๐ Declarations vs assignments
Declarations work directly with var, final, or static.
Non-declaration struct assignments must be wrapped in parentheses.
Array assignments do not need the extra parentheses.
static array destructuring vs static scope access
static[ expr ] is bracket access into the static scope, just like variables[ expr ]. It works for plain and compound assignments.
static [ ... ] = value is an array destructuring declaration only when the brackets clearly hold a pattern: two or more bindings, a trailing comma, a rest binding, a default value, or a nested pattern. A single plain binding is static scope access, with or without a space after static.
๐ฏ Defaults
Defaults let you provide a fallback when a value is not available.
Struct defaults
Struct defaults apply when a key is missing.
Array defaults
Array defaults apply when a slot is missing or null.
๐ช Nested patterns
You can destructure nested structs and arrays.
Nested structs
Nested arrays
You can combine nesting with defaults.
๐งบ Rest bindings
Rest bindings capture the remaining values.
Struct rest
Array rest
Array destructuring also supports middle rest.
Short arrays still bind predictably.
๐ฏ Scoped targets
You can explicitly destructure into scopes by renaming the binding target.
This is useful when you want the source key and target variable to differ.
โ ๏ธ Common errors
Invalid shorthand keys
Some keys cannot use shorthand.
Use explicit renaming for quoted keys, spaced keys, or numeric keys.
Wrong source type
Struct destructuring expects a struct-like value. Array destructuring expects an array-like value.
If a nested value does not match the pattern, BoxLang throws an error.
Multiple rest bindings
A destructuring pattern can only contain one rest binding.
Safer target syntax
Scoped targets must start with an explicit scope. Use variables.foo, request.bar, or another real scope.
โ Summary
Destructuring gives you:
concise assignments
built-in renaming
defaults
nested extraction
rest capture
scoped targets when needed
Last updated
Was this helpful?
