Skip to content

Destructuring ​

One rule in three places: assignment, a match branch and a for header.

Arrays ​

mzs
a, b = [1, 2]
[a, b]  # → [1,2]

user, host = "ivan@example.ru".split("@")
host  # → "example.ru"

first, [second, third] = [1, [2, 3]]
third  # → 3

lo, hi = 1..2
hi  # → 2

Swapping values without a temporary variable:

mzs
a = 1
b = 2
a, b = [b, a]
[a, b]  # → [2,1]

The rest: *rest ​

*name takes everything the others did not into a new array (possibly empty).

mzs
head, *tail = [1, 2, 3]
tail  # → [2,3]

*init, last = [1, 2, 3]
init  # → [1,2]

a, *mid, z = 1..5
mid  # → [2,3,4]

h, *t = [1]
t  # → []

The length must match ​

Extra and missing values are an error, not a silent nil:

mzs
a, b = [1, 2, 3]
text
index: destructuring expects 2 values, got 3
CodeError
a, b = [1]destructuring expects 2 values, got 1
a, *m, z = [1]destructuring expects at least 2 values, got 1
a, b = "ab"cannot destructure string: the right side must be an array
a, b = {x: 1}cannot destructure dict: the right side must be an array

Dicts ​

{…} on the left takes a dict apart by keys. {name} is short for {name: name}.

mzs
user = {name: "Ann", age: 30, city: "Riga"}

{name, age: years} = user
[name, years]  # → ["Ann",30]

{name, **rest} = user
rest  # → {"age":30,"city":"Riga"}

{data: {reason}} = {data: {reason: "defect"}}
reason  # → "defect"

{"content-type": ct} = {"content-type": "text/csv"}
ct  # → "text/csv"

The pattern is open: extra keys are skipped. A named key must be there:

mzs
{phone} = {name: "Ann"}
text
key: destructuring: key not found: "phone"

Arrays and dicts can be mixed:

mzs
[id, {name}] = [7, {name: "Ann"}]
"${id}: ${name}"  # → "7: Ann"

Assignment targets ​

A target is a name, a $variable, an index, a record field or a nested pattern.

mzs
d = {row: 0, col: 0}
d["row"], d["col"] = [12, 4]
d  # → {"row":12,"col":4}

record Point(x, y)
p = Point(0, 0)
p.x, p.y = [3, 4]
p  # → {"x":3,"y":4}

In match ​

In match a mismatch moves on to the next branch instead of failing. * with no name skips elements.

mzs
match [3, 1, 2] { [first, *] -> first; else -> nil }  # → 3
match [] { [first, *] -> first; else -> "empty" }  # → "empty"
match 1 { [a, b] -> "pair"; else -> "not a pair" }  # → "not a pair"
ev = {user: {name: "Ann"}}
match ev { {user: {name}} -> name; else -> "?" }  # → "Ann"

A name in a pattern always binds a new variable. To compare with a variable's value, put it in parentheses:

mzs
limit = 10
row = {n: 10}
match row { {n: (limit)} -> "exactly the limit"; else -> "no" }  # → "exactly the limit"

More on patterns — match.

In for ​

mzs
for k, v in {a: 1, b: 2} { println("${k}=${v}") }
for [id, title] in [[1, "tea"], [2, "coffee"]] { println("${id}: ${title}") }
for {name, age} in [{name: "Ann", age: 30}] { println("${name}, ${age}") }
text
a=1
b=2
1: tea
2: coffee
Ann, 30

As an expression ​

a, b = … is a statement. The bracketed form [a, b] = … is an expression whose value is the right side:

mzs
x = ([a, b] = [1, 2])
x  # → [1,2]