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..100before0..100, the value95takes the first arm. - A
matchmust be exhaustive. It is complete when it ends in anelsearm, 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 reportsmatch must be exhaustive: add an 'else' arm. An open class hierarchy always needs anelsearm. - In a
matchwhose value is used, a block arm yields no value, so it must leave thematchon every path: end it withreturn,throw,breakorcontinue. 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 amatchstatement 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, areadonlyorconstfield 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 anArray<int> | stringsubject); an arm the subject cannot fix, or one that can never match, is a compile error (lang.generics, Type tests). Athissubject narrows too. ANonearm on a subject whose type cannot holdNoneis 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 expressionsubject == valuewould pick for the arm's value, so a class with(==)(Money other)and(==)(int cents)matches an arm150 =>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 forsubject == value. - A range arm
lo..hi =>matches whensubject >= 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 acharcompared againstcharendpoints ('a'..'z') or integer code points (97..122). The endpoints and the subject need not share a type: anintendpoint against afloatsubject promotes like>=does, somatch (2.5) { 0..5 => ... }matches. - A subject the comparison operators do not order against the endpoints (a
bool, a class without both(>=)and(<=), astringagainst a number) is a compile error. - Range arms use the ordering of the comparison operators, so a
NaNsubject matches no range arm and falls toelse,-0.0counts as equal to0.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
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
boolendpoint, 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.