.\" Made by website/plain/build.mjs from website/words/match.md; change that file. .TH SG-MATCH 7 2026-11-28 "SG" "SG reference" .SH NAME match \- chooses by the shape of a value; every case must be covered .SH SYNOPSIS .TP \fBmatch x { P => value }\fR gives the value of the arm that fits .TP \fBmatch x { P => statement }\fR runs the arm that fits .TP \fBP => return value\fR an arm that leaves the function, in a match that gives a value too .TP \fBCircle(r), Rect(w, _)\fR a variant, with a name or _ for each value .TP \fB0, 'a', "stop", true, null\fR a value that must be equal .TP \fB1..=9\fR a range, both ends taken .TP \fBA or B\fR either pattern .TP \fBP when condition\fR the pattern, if the condition holds too .TP \fB_, var name\fR every value left, without or with a name .TP \fBmatch read x, match ref x\fR look into x without moving it .SH DESCRIPTION \fBmatch\fR tries its arms from the top and runs the first whose pattern fits. It works on enums, \fBResult\fR, a \fBT?\fR, numbers, characters, texts and \fBbool\fR\&. Written where a value goes, it gives the value of the arm. .PP Every value must be taken by some arm. A \fBmatch\fR on an enum names each variant or ends with \fB_\fR; on a number or a text, \fB_\fR or \fBvar name\fR takes the rest. A variant added to an enum later makes every \fBmatch\fR that misses it an error, so the compiler shows each place to look at. An arm that can never run is an error too. .PP Arms are written one per line, without commas. An arm with \fBwhen\fR does not count as covering its pattern, since the condition may be false. .PP \fBmatch x\fR moves \fBx\fR when \fBx\fR owns its value, as \fBvar y = x\fR would. \fBmatch read x\fR only looks, and the names of the arms then borrow from \fBx\fR\&. On a value that is already a borrow, the names borrow the same way. .SH EXAMPLES .SS "Numbers" .RS 4 .nf string size(int n) { return match n { 0 => "none" 1..=9 => "a few" 10 or 12 => "ten or a dozen" _ => "many" } } void main() { print($"{size(0)}, {size(4)}, {size(12)}, {size(99)}") } .fi .RE .PP .RS 4 .nf none, a few, ten or a dozen, many .fi .RE .SS "Text" .RS 4 .nf void run(read string command) { match command { "go" => print("going") "stop" or "halt" => print("stopping") _ => print($"what is {command}?") } } void main() { run("go") run("halt") run("jump") } .fi .RE .PP .RS 4 .nf going stopping what is jump? .fi .RE .SS "The variants of an enum" .RS 4 .nf enum Shape { Circle(int radius) Rect(int width, int height) Empty } int area(read Shape shape) { return match shape { Circle(r) => 3 * r * r Rect(w, h) => w * h Empty => 0 } } void main() { var shapes = List { Shape.Circle(2), Shape.Rect(3, 4), Shape.Empty } for shape in read shapes { print(area(shape)) } } .fi .RE .PP .RS 4 .nf 12 12 0 .fi .RE .PP \fBCircle(r)\fR names the value a circle carries; \fBEmpty\fR carries none. .SS "null, and a Result with a pattern inside" .RS 4 .nf nint lengthOf(read string? text) { return match text { null => 0 var t => t.length() } } void main() { print(lengthOf("Pasha")) print(lengthOf(null)) match int.parse("12x") { Ok(n) => print(n) Error(Invalid(at)) => print($"not a number from byte {at}") Error(OutOfRange) => print("too big") } } .fi .RE .PP .RS 4 .nf 5 0 not a number from byte 2 .fi .RE .PP Below the arm \fBnull\fR, \fBt\fR is a \fBread string\fR, not a \fBread string?\fR\&. \fBError(Invalid(at))\fR looks into the error too. .SS "Patterns inside patterns, and when" .RS 4 .nf enum Expr { Number(int value) Add(Expr* left, Expr* right) Mul(Expr* left, Expr* right) } int eval(read Expr e) { return match e { Number(n) => n Add(l, r) => eval(l) + eval(r) Mul(Number(0), _) or Mul(_, Number(0)) => 0 Mul(l, r) => eval(l) * eval(r) } } string show(read Expr e) { return match e { Number(n) when n < 0 => $"({n})".toString() Number(n) => n.toString() Add(l, r) => $"{show(l)} + {show(r)}".toString() Mul(l, r) => $"{show(l)} * {show(r)}".toString() } } void main() { var e = Expr.Add(new Expr.Number(2), new Expr.Mul(new Expr.Number(-3), new Expr.Number(4))) print($"{show(read e)} = {eval(read e)}") } .fi .RE .PP .RS 4 .nf 2 + (-3) * 4 = -10 .fi .RE .PP A pattern looks through the \fBExpr*\fR pointers as through a field. \fBMul(Number(0), _) or Mul(_, Number(0))\fR skips the work when either side is zero; the arm below it takes every other \fBMul\fR\&. .SS "Looking without moving" .RS 4 .nf enum Message { Text(string body) Quit } void main() { var m = Message.Text("hello".clone()) match read m { Text(body) => print(body.length()) Quit => print("quit") } match m { Text(body) => print(body) Quit => print("quit") } } .fi .RE .PP .RS 4 .nf 5 hello .fi .RE .PP The first \fBmatch\fR lends \fBm\fR; the second takes it, and \fBbody\fR then owns the text. .SH "WHAT THE COMPILER REFUSES" .SS "A variant with no arm" .RS 4 .nf enum Shape { Circle(int radius) Rect(int width, int height) Empty } int sides(read Shape shape) { return match shape { Circle(_) => 0 Rect(_, _) => 4 } } void main() { print(sides(read Shape.Empty)) } .fi .RE .PP .RS 4 .nf error: this `match` does not handle `Empty` every variant of `Shape` needs an arm --> example.sg:8:18 8 | return match shape { | ^^^^^ hint: add an arm for each, or `_ => ...` for everything else .fi .RE .SS "An arm that can never run" .RS 4 .nf string size(int n) { return match n { 0 => "none" _ => "some" 1..=9 => "a few" } } void main() { print(size(3)) } .fi .RE .PP .RS 4 .nf error: this arm can never run: an arm above takes every value --> example.sg:5:9 5 | 1..=9 => "a few" | ^^^^^ .fi .RE .SS "A name that is not a variant" .RS 4 .nf enum Color { Red Green } void main() { var c = Color.Green match c { Red => print("red") Gren => print("green") } } .fi .RE .PP .RS 4 .nf error: `Color` has no variant `Gren` --> example.sg:10:9 10 | Gren => print("green") | ^^^^ hint: `Green` .fi .RE .PP A misspelt variant does not quietly take every value: a name that takes the rest is written \fBvar name\fR\&. .SS "A value used after a match took it" .RS 4 .nf enum Message { Text(string body) Quit } void main() { var m = Message.Text("hello".clone()) match m { Text(body) => print(body) Quit => print("quit") } match m { Text(body) => print(body) Quit => print("quit") } } .fi .RE .PP .RS 4 .nf error: `m` was moved and can no longer be used --> example.sg:12:11 12 | match m { | ^ it was moved here: --> example.sg:8:11 8 | match m { | ^ .fi .RE .PP \fBmatch read m\fR the first time keeps \fBm\fR\&. .SH "SEE ALSO" .BR sg-is (7), .BR sg-when (7), .BR sg-or (7), .BR sg-enum (7), .BR sg-try (7) .PP https://sg\-lang.org/reference/match.html