Pattern matching

You can pattern match value using the match expression:

match expression() {
  // you can match constants like numbers, bool, strings, or nil
  42 => "expression is 42",
  [] => "expression is the empty list",
  [hd, ..tl] => "expression is a list with head `hd` and tail `tl`",
  #(x, y) => "matching a tuple",
  #{ } => "matching the empty map",
  #{ "k" => x, ..rest } => "matching a map with a key `k`",
  _ => "catchall clause",
}

The match expression tries to unify the given expression with one clause at a time, and if no clause succeeds it results in a runtime error.

let x = 42;
match x {
  0 => "zero",
  1 => "one",
}

// Execution error:
// No match for 42
//   at <repl>

You can also nest the patterns:

let example = fn x {
  match x {
    [1, 2, third, .._] => third,
    _ => nil
  }
};

example([1, 2, "x", 4, 5]) // => "x"
example([]) // => nil
example(42) // => nil