LEVIATHAN vd52e79c4 · d52e79c486

Expressions

match — type, value and range dispatch

Select the first arm whose pattern matches a value, a type or a range, either as an expression that yields a value or as a statement.

since 0.1.0-alpha.1linuxwindowswasm

Description

match (subject) {
    pattern => body;
    pattern => { block }
    else => body;
}

A match compares subject with each arm in source order and runs the first arm that matches; later arms are not examined. A pattern is one of:

  • a type (Dog =>): matches when the subject is an instance of that type, and narrows the subject to that type inside the arm;
  • a value (0 =>, Color::Red =>): matches when the subject equals the value;
  • a range (1..9 =>): matches when the subject lies between the two endpoints, both endpoints included;
  • else: matches anything that reached it.

An arm body is either an expression followed by ; or a block in braces. match is an expression when its value is used (the arms then yield the value) and a statement when it stands alone. With a target, each expression arm is checked against it; without one, the expression arms merge (lang.merged-types) and a block arm yields no value. A match statement needs no trailing ; after its closing brace.

match is a reserved word.

Type arms, value arms and range arms

use io::console;

class Animal { }
class Dog : Animal { }
class Cat : Animal { }

string describe(Animal a) {
    return match (a) {
        Dog => "a dog";
        Cat => "a cat";
        else => "some other animal";
    };
}

string size(int n) {
    return match (n) {
        0 => "zero";
        1..9 => "small";
        10..99 => "medium";
        else => "large";
    };
}

console.writeln(describe(Dog()));
console.writeln(describe(Animal()));
console.writeln(size(0));
console.writeln(size(9));
console.writeln(size(10));
console.writeln(size(-4));
a dog
some other animal
zero
small
medium
large

Rules

  • Arms are tried in source order and the first match wins. When two arms overlap, the earlier one decides: with 90..100 before 0..100, the value 95 takes the first arm.
  • A match must be exhaustive. It is complete when it ends in an else arm, when its subject has a closed union type and every member of the union has an arm, or when its subject is an enum and every member of the enum has an arm. Otherwise the compiler reports match must be exhaustive: add an 'else' arm. An open class hierarchy always needs an else arm.
  • In a match whose value is used, a block arm yields no value, so it must leave the match on every path: end it with return, throw, break or continue. A block arm whose end can be reached is a compile error naming the arm (the 'else' arm of a value-producing 'match' can complete without a value). In a match statement a block arm may end normally.
  • A type arm narrows the subject: inside the arm the subject has the arm's type, so its members can be used directly. Only a subject that can be narrowed narrows (a local, a parameter, this, a readonly or const field path, a top-level variable no function assigns; lang.unions-and-optionals). On any other subject, such as a mutable field (match (o.f)), the arm still selects by the run-time type, but inside it the subject keeps its declared type: copy it into a local and match on the local to use the arm's members. A type arm naming a generic class is written with the type arguments the subject's type fixes (Array<int> => for an Array<int> | string subject); an arm the subject cannot fix, or one that can never match, is a compile error (lang.generics, Type tests). A this subject narrows too. A None arm on a subject whose type cannot hold None is a compile error ("'v' can never be None: its type 'int | string' is not optional").
  • A value arm matches when subject == value. When the subject is an object whose class declares (==), the arm runs the (==) overload that the expression subject == value would pick for the arm's value, so a class with (==)(Money other) and (==)(int cents) matches an arm 150 => through (==)(int cents). As in that expression, a subclass's override of the overload runs when the subject holds that subclass (lang.method-dispatch). An arm value that none of the class's (==) overloads accepts is a compile error, as it is for subject == value.
  • A range arm lo..hi => matches when subject >= lo && subject <= hi. Both endpoints are included, and an arm whose first endpoint is larger than its second (5.0..0.0) matches nothing. On an object subject the arm runs the (>=) and (<=) overloads those two comparisons would pick for the endpoints, and their overrides, as they would.
  • Range arms work for every subject that the comparison operators order: all the integer widths, the float types, string (compared lexicographically), and a char compared against char endpoints ('a'..'z') or integer code points (97..122). The endpoints and the subject need not share a type: an int endpoint against a float subject promotes like >= does, so match (2.5) { 0..5 => ... } matches.
  • A subject the comparison operators do not order against the endpoints (a bool, a class without both (>=) and (<=), a string against a number) is a compile error.
  • Range arms use the ordering of the comparison operators, so a NaN subject matches no range arm and falls to else, -0.0 counts as equal to 0.0, and an infinity lies outside every finite range.
  • A name written with :: in an arm is resolved by what it names: a type qualified by its namespace is a type pattern, while an enum member or a namespace constant is a value pattern.

Examples

A match over a union, narrowing in each arm

use io::console;

string show(int | string v) {
    return match (v) {
        int => "int ${v + 1}";
        string => "string of length ${v.length()}";
    };
}

console.writeln(show(41));
console.writeln(show("hello"));
int 42
string of length 5

Range arms on floats, strings and chars

use io::console;

string temperature(float t) {
    return match (t) {
        0.0..15.0 => "cold";
        15.0..30.0 => "mild";
        else => "out of range";
    };
}

string half(string s) {
    return match (s) {
        'a'..'m' => "first half";
        else => "second half";
    };
}

string kind(char c) {
    return match (c) {
        48..57 => "digit";
        97..122 => "lowercase letter";
        else => "other";
    };
}

console.writeln(temperature(2.5));
console.writeln(temperature(15.0));
console.writeln(temperature(99.0));
console.writeln(half("cat"));
console.writeln(half("zebra"));
char q = 'q';
console.writeln(kind(q));
char seven = '7';
console.writeln(kind(seven));
cold
cold
out of range
first half
second half
lowercase letter
digit

Enum members, namespace constants and the statement form

use io::console;

enum Color { Red, Green, Blue }

namespace Limits { const int Max = 3; }

string name(Color c) {
    return match (c) {
        Color::Red => "red";
        Color::Green => "green";
        Color::Blue => "blue";
    };
}

string check(int n) {
    return match (n) {
        Limits::Max => "at the limit";
        else => "below or above";
    };
}

console.writeln(name(Color::Green));
console.writeln(check(3));

match (3) {
    1 => console.writeln("one");
    3 => { console.writeln("three"); }
    else => console.writeln("other");
}
green
at the limit
three
use io::console;

int n = 7;
match (n) {
    1 => console.writeln("one");
    2 => console.writeln("two");
}
// error: match must be exhaustive: add an 'else' arm
not run — shows a compile error

Notes

  • A range arm's endpoints are not checked against the subject's type. An arm whose subject has no ordering, such as an array or a bool endpoint, compiles but behaves differently from one engine to another, so keep range arms to the subjects listed in the rules.

See also

  • Exception — The base class of the standard exceptions, holding a message.
  • Exceptions: throw, try and catch — Throwing a value that implements IException, catching it by type, and the standard exception hierarchy.