.\" Made by website/plain/build.mjs from website/words/when.md; change that file. .TH SG-WHEN 7 2026-11-28 "SG" "SG reference" .SH NAME when \- adds a condition to an arm of a match, or makes a lock block wait until one holds .SH SYNOPSIS .TP \fBP when condition => ...\fR an arm that runs when P fits and the condition holds .TP \fBCircle(r) when r > 10 => ...\fR the names of the pattern are seen in the condition .TP \fBA or B when condition => ...\fR the condition belongs to the whole arm, not to B .TP \fBvar x when condition => ..., _ when condition => ...\fR a condition on any value .TP \fBlock v as name when condition { ... }\fR waits, with the lock let go, until the condition holds .TP \fBawait lock v as name when condition { ... }\fR the same in an async function .SH DESCRIPTION In a \fBmatch\fR, \fBwhen\fR adds a condition to an arm. The arm runs when its pattern fits and its condition is \fBtrue\fR; when the condition is \fBfalse\fR, the arms below are tried. The condition is a \fBbool\fR, computed only when the pattern fits, and it sees the names the pattern gives and every variable around the \fBmatch\fR\&. .PP An arm with \fBwhen\fR does not count as covering its pattern, since the condition may be false. A \fBmatch\fR still needs arms that take every value without a condition: most often the same pattern again, below, or \fB_\fR\&. An arm with \fBwhen\fR under an arm that already takes all its values can never run, and is an error. .PP The condition only looks. It cannot move a value, since the arms below still need it when the condition fails; it reads, or lends with \fBread\fR\&. With \fBor\fR, the condition is computed once, with the names of the first pattern that fits. Below \fBnull when ...\fR, a name is still a \fBT?\fR: only an arm \fBnull\fR without a condition takes \fBnull\fR away. .PP In a \fBlock\fR block, \fBwhen\fR makes the task wait. The condition is computed under the lock; while it fails, the lock is let go and the task sleeps, and it looks again each time a \fBlock\fR block on the same value ends. Then the block runs under the lock. The condition reads only the locked values. .PP \fBwhen\fR has no place in an \fBis\fR: an \fBif\fR joins a test and a condition with \fB&&\fR\&. .PP \fBwhen\fR is a word only in those two places, after the pattern of an arm and after what a \fBlock\fR holds. Elsewhere it is a name like any other: \fBvar when = DateTime.fromUnixSeconds(0)\fR is a variable, and \fB_ when when > 2 => ...\fR reads it. .SH EXAMPLES .SS "A condition on an arm" .RS 4 .nf enum Shape { Circle(int radius) Rect(int width, int height) Empty } string describe(read Shape shape) { return match shape { Circle(r) when r > 10 => "a big circle" Circle(_) => "a circle" Rect(w, h) when w == h => "a square" Rect(_, _) => "a rectangle" Empty => "nothing" } } void main() { print(describe(read Shape.Circle(20))) print(describe(read Shape.Circle(2))) print(describe(read Shape.Rect(3, 3))) print(describe(read Shape.Rect(3, 4))) print(describe(read Shape.Empty)) } .fi .RE .PP .RS 4 .nf a big circle a circle a square a rectangle nothing .fi .RE .PP \fBr\fR, \fBw\fR and \fBh\fR are named by the pattern and seen in the condition. A circle that is not big falls to the plain \fBCircle(_)\fR below. .SS "The condition runs only when the pattern fits" .RS 4 .nf bool even(int n) { print($"is {n} even?") return n % 2 == 0 } string kind(int n) { return match n { 0 => "zero" 1..=9 when even(n) => "small and even" 1..=9 => "small" _ => "big" } } void main() { print(kind(0)) print(kind(4)) print(kind(7)) print(kind(50)) } .fi .RE .PP .RS 4 .nf zero is 4 even? small and even is 7 even? small big .fi .RE .PP \fBeven\fR is called for 4 and 7, which fit \fB1..=9\fR, and not for 0 or 50. .SS "Variables around the match" .RS 4 .nf void main() { var limit = 10 var fields = List { "4", "40", "x" } for field in read fields { match int.parse(field) { Ok(n) when n > limit => print($"{n} is over {limit}") Ok(n) => print($"{n} is fine") Error(_) => print($"{field} is not a number") } } } .fi .RE .PP .RS 4 .nf 4 is fine 40 is over 10 x is not a number .fi .RE .SS "null, and a name below it" .RS 4 .nf nint score(read string? name) { return match name { null => 0 var n when n.length() > 5 => 99 var n => n.length() } } void main() { print(score(null)) print(score("Pasha")) print(score("Augusta")) } .fi .RE .PP .RS 4 .nf 0 5 99 .fi .RE .PP Below the arm \fBnull\fR, \fBn\fR is a \fBread string\fR, so the condition can ask for its \fBlength()\fR\&. .SS "when after or" .RS 4 .nf enum Shape { Circle(int radius) Square(int side) Empty } string describe(read Shape shape) { return match shape { Circle(n) or Square(n) when n > 10 => $"big, {n} across".toString() Circle(_) or Square(_) => "small" Empty => "nothing" } } void main() { print(describe(read Shape.Square(12))) print(describe(read Shape.Circle(3))) print(describe(read Shape.Empty)) } .fi .RE .PP .RS 4 .nf big, 12 across small nothing .fi .RE .PP The condition is on the whole arm: a big circle and a big square both take it. .SS "The first pattern that fits" .RS 4 .nf enum Pair { Two(int a, int b) } void main() { var p = Pair.Two(2, 7) match p { Two(x, 1..=9) or Two(1..=9, x) when x > 5 => print($"{x} is over 5") Two(a, b) => print($"{a} and {b}") } } .fi .RE .PP .RS 4 .nf 2 and 7 .fi .RE .PP Both patterns fit \fBTwo(2, 7)\fR\&. The first gives \fBx = 2\fR, and the condition is computed once, with that \fBx\fR; it fails, so the arm below runs. The second pattern, with \fBx = 7\fR, is not tried. So a pattern of an \fBor\fR that the patterns before it take is an error with a \fBwhen\fR too: \fBTwo(x, _) or Two(_, x) when x > 5\fR takes every pair by its first pattern, and the condition never sees the \fBx\fR of the second. .SS "Looking in the condition, taking in the arm" .RS 4 .nf enum Message { Text(string body) Quit } bool isLong(read string text) { return text.length() > 3 } void keep(string body) { print($"kept {body}") } void main() { var m = Message.Text("hello".clone()) match m { Text(body) when isLong(read body) => keep(body) Text(body) => print(body) Quit => print("quit") } } .fi .RE .PP .RS 4 .nf kept hello .fi .RE .PP The condition lends \fBbody\fR with \fBread\fR; the arm then owns it and gives it away. .SS "Waiting under a lock" .RS 4 .nf struct Box { int value bool ready } void fill(shared locked Box box) { lock box as b { b.value = 42 b.ready = true } } void main() { var box = new shared locked Box { value = 0, ready = false } var task = Task.run(fill, share box) lock box as b when b.ready { print(b.value) } task.wait() } .fi .RE .PP .RS 4 .nf 42 .fi .RE .PP \fBmain\fR waits until \fBfill\fR has set \fBready\fR\&. The end of \fBfill\fR\&'s \fBlock\fR block wakes it; no task has to wake another by hand. .SH "WHAT STOPS THE PROGRAM" .SS "Waiting for what nobody can change" .RS 4 .nf struct Box { int value bool ready } void main() { var box = new shared locked Box { value = 0, ready = false } lock box as b when b.ready { print(b.value) } } .fi .RE .PP .RS 4 .nf panic: this task waits until `b.ready`, and no other task could make it hold: it is the only owner of `box` .fi .RE .PP No other task holds \fBbox\fR, so \fBready\fR could never change. A task that has no more to give sets a field such as \fBdone\fR in a \fBlock\fR block, and the condition looks at it: \fBwhen b.ready || b.done\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "An arm with when as the only arm of a variant" .RS 4 .nf enum Shape { Circle(int radius) Empty } string describe(read Shape shape) { return match shape { Circle(r) when r > 10 => "a big circle" Empty => "nothing" } } void main() { print(describe(read Shape.Empty)) } .fi .RE .PP .RS 4 .nf error: this `match` does not handle `Circle` an arm with `when` does not settle its variant: the condition may be false --> example.sg:7:18 7 | return match shape { | ^^^^^ hint: add an arm for each, or `_ => ...` for everything else .fi .RE .SS "An arm below one that takes all its values" .RS 4 .nf enum Shape { Circle(int radius) Empty } void main() { var s = Shape.Circle(3) match s { Circle(_) => print("a circle") Circle(r) when r > 1 => print("a big circle") Empty => print("nothing") } } .fi .RE .PP .RS 4 .nf error: this arm can never run: `Circle` is already handled above --> example.sg:10:9 10 | Circle(r) when r > 1 => print("a big circle") | ^^^^^^^^^ .fi .RE .PP The arm with the condition goes first. .SS "Moving a value in the condition" .RS 4 .nf enum Message { Text(string body) Quit } bool keep(string body) { print(body) return true } void main() { var m = Message.Text("hi".clone()) match m { Text(body) when keep(body) => print("kept") Text(body) => print(body) Quit => print("quit") } } .fi .RE .PP .RS 4 .nf error: `body` cannot be moved in a `when` condition if the condition does not hold, the arms below still need the value --> example.sg:14:30 14 | Text(body) when keep(body) => print("kept") | ^^^^ hint: a condition only looks: lend the value with `read`, or test it inside the arm .fi .RE .SS "A name below null when" .RS 4 .nf nint score(read string? name) { return match name { null when name is null => 0 var n => n.length() } } void main() { print(score("ab")) } .fi .RE .PP .RS 4 .nf error: this is `read string?` and may be `null`; `.length()` needs a value the arm `null` above has a `when`, so it does not take `null` away: its condition may be false --> example.sg:4:18 4 | var n => n.length() | ^ hint: test it first: `if n is not null { ... }` .fi .RE .PP The arm \fBnull\fR has a condition, so \fBnull\fR may still reach \fBvar n\fR\&. A plain \fBnull => 0\fR makes \fBn\fR a \fBread string\fR\&. .SS "when after is" .RS 4 .nf enum Shape { Circle(int radius) Empty } void main() { var s = Shape.Circle(4) if s is Circle(r) when r > 2 { print("a big circle") } } .fi .RE .PP .RS 4 .nf error: expected `{`, found `when` `when` is the condition of an arm of a `match`; after `is`, a condition is joined with `&&` --> example.sg:8:23 8 | if s is Circle(r) when r > 2 { | ^^^^ hint: `if s is Circle(r) && r > 2` .fi .RE .PP In an \fBif\fR, the test and the condition are joined with \fB&&\fR: \fBif s is Circle(r) && r > 2\fR\&. .SS "A condition that reads the clock" .RS 4 .nf struct Box { long deadline } void take(shared locked Box box) { lock box as b when b.deadline < Instant.now().elapsed().milliseconds() { b.deadline = 0 } } void main() { take(new shared locked Box { deadline = 0 }) } .fi .RE .PP .RS 4 .nf error: the condition of `when` calls `Instant.now`, which reads the clock, and a task waiting for it is woken only when a `lock` block on the same value ends a change outside the locked value would not wake it, and it would wait for ever .fi .RE .SH "SEE ALSO" .BR sg-match (7), .BR sg-or (7), .BR sg-is (7), .BR sg-null (7), .BR sg-lock (7) .PP https://sg\-lang.org/reference/when.html