Skip to main content

Pattern matching

Overview

Pattern matching is a language feature that makes it easy to both test and decompose structured data into its constituent parts. While most programming languages provide familiar ways to build structured data, pattern matching enables you to take apart structured data and bring its fragments into scope by binding them to the names you specify. Syntactically, the patterns resemble the construction of structured data, but generally appear in input-direction positions, such as in function argument positions, after the case keyword in switch expressions, and after let or var declarations.

Types of patterns

The following table summarizes the different ways of pattern matching.

Pattern kindExample(s)ContextCan failRemarks
Literalnull, 42, (), "Hi"EverywhereWhen the type has more than one value
Namedage, xEverywhereNoIntroduces identifiers into a new scope
Wildcard_EverywhereNo
Typedage : NatEverywhereConditional
Option?0, ?valEverywhereYesSee also option blocks
Tuple( component0, component1, …​ )EverywhereConditionalMust have at least two components
Object{ fieldA; fieldB; …​ }EverywhereConditionalAllowed to mention a subset of fields
Fieldage, count = 0ObjectConditionalage is short for age = age
Variant#celsius deg, #sundayEverywhereYes#sunday is short form for #sunday ()
Alternative (or-pattern)0 or 1EverywhereDependsNo alternative may bind an identifier

Using pattern matching

Consider the following function call:

let name : Text = fullName({ first = "Jane"; mid = "M"; last = "Doe" });

This code constructs a record with three fields and passes it to the function fullName. The result of the call is named and brought into scope by binding it to the identifier name. The last, binding step is called pattern matching, and name : Text is one of the simplest forms of a pattern. For instance, in the following implementation of the callee:

func fullName({ first : Text; mid : Text; last : Text }) : Text {
first # " " # mid # " " # last
};

The input is an anonymous object which is destructured into its three Text fields, whose values are bound to the identifiers first, mid and last. They can be freely used in the block that forms the body of the function. Above we have resorted to name punning, a form of aliasing for object field patterns, using the name of a field to also name its contents. A more general form of field patterns allows the content to be named separately from the field, as in …​; mid = m : Text; …​. Here mid determines which field to match, and m names the content of that field within the scope of the pattern.

Literal patterns

You can also use pattern matching to declare literal patterns, which look just like literal constants. Literal patterns are especially useful in switch expressions because they can cause the current pattern match to fail, and thus start to match the next pattern. For example:

switch ("Adrienne", #female) {
case (name, #female) { name # " is a girl!" };
case (name, #male) { name # " is a boy!" };
case (name, _) { name # ", is a human!" };
}

This program will match the first case clause because binding to the identifier name cannot fail and the shorthand variant literal #female compares as equal. Then it evaluates to "Adrienne is a girl!". The last clause showcases the wildcard pattern _. It cannot fail, but won’t bind any identifier.

or patterns

The last kind of pattern is the or pattern. As its name suggests, these are two or more patterns that are separated by the keyword or. Each of the sub-patterns must bind to the same set of identifiers, and is matched from left-to-right. An or pattern fails when its rightmost sub-pattern fails.

More on patterns

Since pattern matching has a rich history and interesting mechanics, a few additional comments are justified.

Terminology

The expression whose value is being matched is frequently called the scrutinee. The patterns appearing behind the keyword case are the alternatives. When every possible value of the scrutinee is matched by at least one alternative, then the scrutinee is covered. The alternatives are tried in order. In case of overlapping patterns, the earlier one is selected. An alternative is considered dead or redundant if for every value that it matches there is already some earlier alternative that also matches the value.

Booleans

The data type Bool can be regarded as two disjointed alternatives (true and false) and Motoko’s built-in if construct will eliminate the data and turn it into control flow. if expressions are a form of pattern matching that abbreviates the general switch expression for the special case of boolean scrutinees.

Variant patterns

Motoko’s variant types are a form of disjoint union, sometimes also called a sum type. A value of variant type always has exactly one discriminator and a payload which can vary from discriminator to discriminator. When matching a variant pattern with a variant value, the discriminators must be the same in order to select the alternative, and if so, the payload gets exposed for further matching.

Enumerated types

Other programming languages often use a keyword enum to define discrete enumerations. These are poor relations of Motoko’s more general variant types, as the alternatives of an enumeration are not allowed to carry any payload. Correspondingly, in those languages the switch-like statements used to analyse enum values lack the full power of pattern matching. Motoko provides the short-hand syntax, as in type Weekday = { #mon; #tue; …​ }, to define basic enumerations for which no payloads are required.

Error handling

Error handling can be considered a use-case for pattern matching. When a function returns a value that has an alternative for success and one for failure, pattern matching can be used to distinguish between the two as discussed in error handling.

Irrefutable patterns

A pattern is refutable if matching some value of the expected type against it can fail. Literal and variant patterns are generally refutable, since they require an equal value or variant tag and these could fail to match.

A pattern that cannot fail to match every value is irrefutable. Examples of irrefutable patterns are the wildcard pattern _, identifier patterns x and tuple or record patterns built from irrefutable sub-patterns.

Singleton types

Some types contain just a single value. We call these singleton types. Examples of these are the unit type, also known as an empty tuple, or tuples of singleton types. Variants with a single tag and with no or a singleton type payload are singleton types too. Pattern matching on singleton types is particularly straightforward, as it only has one possible outcome of a successful match.

Exhaustiveness (coverage) checking

At runtime, a switch expression may wind up scrutinizing a value to which none of its alternative patterns apply, generating an undesired trap. To detect the possibility of such runtime failures, the Motoko compiler checks for the exhaustiveness of pattern matching by keeping track of the covered shape of the scrutinee. The compiler issues a warning for any non-covered scrutinees. Motoko even constructs a helpful example of a scrutinee that is not matched. A useful by-product of the exhaustiveness check is that it identifies and warns the developer about dead or redundant alternatives that can never be matched.

Refutable patterns and dealing with non-matching data

Developers may only be interested in specially formed data with a desire to directly handle all non-matching forms. The let-else construct is designed precisely for this purpose. Whereas the regular destructuring let allows to focus on a single given pattern, it invariably traps if the right-hand side data doesn't match it, which is warned at compilation. The else clause gives the programmer a way to deal with refuted matches, such as bailing out of the process or logging a message before trapping. As such, let-else is similar to a two-case switch in a compact form that additionally doesn't force the indentation of the processing logic following it.

The below example illustrates how you can write a non-indenting if-else by resorting to a let-else in your code:

let true = isLoggedIn(customer) else return;
// process message for logged-in customer

The expression (or block) following the else must be of type None signifying that its execution trajectory mustn't contribute to the code immediately following the let declaration.

Option blocks for streamlined processing of optional data

Pattern matching on optional data (of type ?T) is a preferred technique for avoiding the dreaded null-exeption problems known from other languages. However, switch-ing  on several options can lead to tedious coding and deeply nested sources. To remedy these problems, Motoko provides option blocks (do ? { ... }) that allow safe unwrapping of options using a postfix ! operator. Every use of the ! in the block corresponds to a switch on some option, with the additional short-circuiting behavior that if ! is applied to a null value, the entire block stops evaluation and immediately returns null.