Assignment Expressions

An assignment expressions consists of the character = immediately followed by a pattern. It binds the names in the pattern to the corresponding parts of its input value so you can reuse them later in the program. A pattern can be a name, an array literal where the elements are patterns, or an object literal where the values are patterns.

ProgramTypeValueError
1 +1 =a 3 *2 +aNum8
1 +1 ==2 =p 1 +1 ==1 =q p ==q notBooltrue
[1, 2, 3] =[a, b, c] aNum1
[1, 2, 3] =[a;r] rArr<Num, Num>[2, 3]
{a: 1, b: 2, c: 3} ={a: d, b: e, c: f} dNum1
{a: 1, b: 2, c: 3} ={a: d, b: e, c: f} eNum2
{a: 1, b: 2, c: 3} ={a: d, b: e, c: f} fNum3
for Num def cube Num as =n *n *n ok 3 cubeNum27

An “impossible match” error occurs if the pattern cannot match any values of the input type, e.g., if an array pattern has a different length from the input, or matching objects with array patterns, or vice versa, or if an object pattern contains keys that the input doesn’t.

ProgramTypeValueError
[1, 2, 3] =[a, b]Type error
Code: ImpossibleMatch
Message: Impossible match. The pattern will never match the input type.
{a: 1, b: 2, c: 3} =[a, b]Type error
Code: ImpossibleMatch
Message: Impossible match. The pattern will never match the input type.
{a: 1, b: 2, c: 3} ={g: h}Type error
Code: ImpossibleMatch
Message: Impossible match. The pattern will never match the input type.

A “nonexhaustive match” error occurs if the pattern can match some but not all values of the input type, e.g., if a variable-length array type is matched as fixed-length, or if an object type is matched against a key it might or might not contain.

ProgramTypeValueError
for Arr<Num...> def f Num as =[a, b] a okType error
Code: NonExhaustiveMatch
Message: Match is not exhaustive. Consider adding elis clauses and/or an else clause.
for Obj<a: Num, Num> def f Num as ={a: a, b: b} a +b okType error
Code: NonExhaustiveMatch
Message: Match is not exhaustive. Consider adding elis clauses and/or an else clause.
for Obj<a: Num, Num> def f Num as ={b: b} b okType error
Code: NonExhaustiveMatch
Message: Match is not exhaustive. Consider adding elis clauses and/or an else clause.