Skip to content

Keyword: match

The match keyword in Filtrera is used for pattern matching, allowing you to execute code based on the structure and values of data. This reference guide provides a detailed specification of the match keyword, focusing on its syntax.

The match keyword enables you to compare a value against a series of patterns and execute the corresponding code for the first pattern that matches. Patterns can include type constraints, value constraints, and combinations of both.

Syntax

Basic Syntax

match <value>
<pattern1> |> <expression1>
<pattern2> when <condition> |> <expression2>
...
  • <value>: The value to be matched against the patterns.
  • <pattern>: The pattern to match the value against.
  • <condition>: Optional condition that must match.
  • <expression>: The expression to execute if the pattern matches.

Example

let value = 42
from value match
(x: text) |> $'It is a text: {x}'
(x: number) |> $'It is a number: {x}'
(x: boolean) |> $'It is a boolean: {x}'

In this example, value is matched against three patterns: text, number, and boolean. Since value is a number, the second pattern matches and the corresponding expression is executed.

Detailed Syntax

Type Constraints

Patterns can include type constraints to match values of specific types. If the match is done against a symbol, that symbol will be assumed to be the matched type inside the case branch.

from value match
text |> $'Text: {value}'
number |> $'Doubled Number: {value * 2}'

Value Constraints

Literal types can be used to match specific values:

from value match
42 |> 'The answer to everything'
|> 'Nothing'

Default Case

A default case can be added using a pattern without any constraints to match any value that does not match the previous patterns.

from value match
(x: text) |> $'Text: {x}'
(x: number) |> $'Number: {x}'
|> 'Other type'

In this example, the last pattern matches any value that is not a text or number.

Practical Usage

Example: Matching Complex Types

let person = { name = 'Alice', age = 30 }
from person match
(x: { name: text, age: number }) |> $'Name: {x.name}, Age: {x.age}'
|> 'Unknown type'

In this example, person is matched against a pattern that requires it to be an record with name and age fields.

Example: Matching with Default Case

param value = true
from value match
(x: text) |> $'Text: {x}'
(x: number) |> $'Number: {x}'
|> 'Other type'

In this example, value does not match the text or number patterns, so the default case is executed, resulting in 'Other type'.

Example: Matching with a Logical Condition

param value: number
from value match
when value > 40 and value < 45 |> 'Value has potential'
|> 'Value is not the answer'

Summary

The match keyword in Filtrera provides a powerful way to handle different data structures and values through pattern matching. By understanding its syntax, including type constraints, value constraints, combined constraints, default cases, and negated types, you can leverage the match keyword to create robust and flexible programs.