Skip to main content

Match assignment expression

k# use = as and match assignment operator. You can match values to variables, or the elements of a list, map or types. All match assignments are defined using let or match expressions

Match a value to a variable (binding)

x = 10

Match a variable to a value

10 = x

Match tuples

x, y = 10, 20

Match lists

[x, y] = [10, 20]

You can also match many arguments and the rest in a variable

[1, 2 | others] = [1, 2, 3]
3 = others //this is true

Match types

Bool x = Bool True

If type have labels you can also use the labels to partial match a type

Username password: p = Username name: "user1" password: "12345678"

Matching sets

With sets the match check the expression at the right contains the elements defined in the expression at the left

#[1, 2] = x // match x contains 1 and 2

Matching maps

The keys matched must be exists in the map expression at the right

{"key1": x, "key2": y} = {"key1": "value"} //error key2 is not in the map expression at the right

Conditional matches

It is possible to use && and || to add guards to matches

x && isEven x = 10 //bind 10 to x if the binding isEven

Grouping matches

You can group conditions using parenthesis

x && (isEven x || isLessThan x 9) = 10 //bind 10 to x if the binding isEven and isLessThan 9
note

the match value (left expression) must be a constant value. That include functions that always return a constant value or is evaluated to a constant value.