top of page
Pattern Matching
A function can be given multiple definitions to handle special cases or differentiate between the types of the arguments.
​
def factorial 0 = 1
def factorial n = n * factorial (n - 1)
​
When a function is called each definition will be checked from top to bottom and if the argument matches the corresponding definition will get called.
​
Pattern matching can be done on primitive type values, lists, tuples, structs and types in general. Just a variable (including _) will always match with any value.
​
Using ... as the last element of an array, list or tuple signifies that it could contain more items.
For example match [int _, ...] [1, 2 ,3] is true.
bottom of page