.\" Made by website/plain/build.mjs from website/words/or.md; change that file. .TH SG-OR 7 2026-11-28 "SG" "SG reference" .SH NAME or \- joins patterns: the arm, or the is, fits when one of them does .SH SYNOPSIS .TP \fBRed or Green => ...\fR an arm for either variant .TP \fB10 or 12, '+' or '\-', "stop" or "halt"\fR numbers, characters and texts .TP \fB\&'a'..='z' or 'A'..='Z' or '_'\fR ranges and values together .TP \fBnull or 0\fR on a T?: null, or a value that must be equal .TP \fBAdd(l, ) or Mul(l, )\fR each pattern names the same values, with the same types .TP \fBNumber(1..=9 or 40), Word("if" or "else")\fR inside the parentheses of a variant .TP \fBx is Plus or Minus\fR in an is; x is not A or B is true when neither fits .TP \fBA or at the end of a line\fR the arm goes on in the next line .SH DESCRIPTION \fBor\fR joins two patterns or more into one. The arm runs, or the \fBis\fR is true, when one of them fits; they are tried from the left. It goes wherever a pattern goes: in an arm of a \fBmatch\fR, in an \fBis\fR, and inside the parentheses of a variant, at any depth. .PP A pattern of an \fBor\fR can name values, and then every pattern of it names the same values with the same types: whichever fits, the arm finds each name set. A pattern that the arms above and the patterns before it take already can never match, and is an error. .PP \fBor\fR joins patterns, not values. Two \fBbool\fR values are joined with \fB||\fR, and \fB|\fR is the \fBor\fR of the bits of two numbers; between patterns, \fB|\fR and \fB||\fR are errors whose hint is \fBor\fR\&. Since the words differ, a pattern and a value read apart: \fBx is A or B || ready\fR\&. .PP A \fBwhen\fR after an \fBor\fR is on the whole arm, and it is computed once, with the names of the first pattern that fits; the patterns after it are not tried when it fails. A pattern the ones before it take is an error with a \fBwhen\fR too. \fBx is not A or B\fR tests the whole \fBor\fR: it is true when neither \fBA\fR nor \fBB\fR fits. .PP \fBor\fR is a keyword everywhere: no variable is named \fBor\fR\&. \fBwhen\fR and \fBnot\fR are words only in their places, and names elsewhere. .SH EXAMPLES .SS "The variants of an enum" .RS 4 .nf enum Op { Plus Minus Times Over Power } string kind(Op op) { return match op { Plus or Minus => "adds" Times or Over or Power => "multiplies" } } void main() { print(kind(Op.Minus)) print(kind(Op.Power)) } .fi .RE .PP .RS 4 .nf adds multiplies .fi .RE .PP A line that ends with \fBor\fR goes on in the next. Every variant has an arm, so the \fBmatch\fR needs no \fB_\fR\&. .SS "Numbers and texts" .RS 4 .nf string sizeOf(int n) { return match n { 0 => "none" 1 or 2 or 3 => "a few" 10 or 12 or 20..=29 => "tens" _ => "many" } } void main() { var commands = List { "stop", "quit", "go" } for command in read commands { match command { "stop" or "halt" or "quit" => print("stopping") _ => print($"{command}?") } } print($"{sizeOf(2)}, {sizeOf(12)}, {sizeOf(25)}, {sizeOf(99)}") } .fi .RE .PP .RS 4 .nf stopping stopping go? a few, tens, tens, many .fi .RE .SS "Ranges of characters" .RS 4 .nf string kind(char c) { return match c { 'a'..='z' or 'A'..='Z' or '_' => "letter" '0'..='9' => "digit" ' ' or '\et' or '\en' => "space" _ => "other" } } void main() { print($"{kind('q')} {kind('_')} {kind('7')} {kind('\et')} {kind('+')}") } .fi .RE .PP .RS 4 .nf letter letter digit space other .fi .RE .SS "null or a value" .RS 4 .nf string show(int? count) { return match count { null or 0 => "none" var n => $"{n + 1}".toString() } } void main() { print(show(3)) print(show(0)) print(show(null)) } .fi .RE .PP .RS 4 .nf 4 none none .fi .RE .PP Below an arm that takes \fBnull\fR, \fBn\fR is an \fBint\fR, so \fBn + 1\fR needs no test. .SS "Names in an or" .RS 4 .nf enum Note { Text(string body) Title(string body) Picture(int width, int height) } void keep(string body) { print($"kept {body}") } void main() { var notes = List { Note.Title("Ships".clone()), Note.Picture(640, 480), Note.Text("ahoy".clone()) } for note in notes { match note { Text(body) or Title(body) => keep(body) Picture(w, h) => print($"a picture, {w} by {h}") } } } .fi .RE .PP .RS 4 .nf kept Ships a picture, 640 by 480 kept ahoy .fi .RE .PP Both patterns name \fBbody\fR, a \fBstring\fR in each, so one arm serves the two variants. The \fBmatch\fR takes \fBnote\fR, and \fBbody\fR owns its text. .SS "In an is, and inside the parentheses" .RS 4 .nf enum Token : Formattable { Number(int value) Word(string text) Plus Minus } void main() { var tokens = List { Token.Plus, Token.Number(7), Token.Word("if".clone()), Token.Number(40) } for token in read tokens { if token is Plus or Minus { print("an operator") } if token is Number(1..=9 or 40) or Word("if" or "else") { print($"small, forty, or a keyword: {token}") } if token is not Number(_) or Word(_) { print("neither a number nor a word") } } } .fi .RE .PP .RS 4 .nf an operator neither a number nor a word small, forty, or a keyword: Number(7) small, forty, or a keyword: Word("if") small, forty, or a keyword: Number(40) .fi .RE .PP Inside the parentheses, \fBor\fR tests the value the variant carries. \fBis not\fR takes the whole \fBor\fR\&. .SS "Names in an is, and when" .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() { var shapes = List { Shape.Circle(2), Shape.Square(15), Shape.Empty } for shape in read shapes { if shape is Circle(n) or Square(n) && n > 3 { print($"more than 3: {n}") } print(describe(shape)) } } .fi .RE .PP .RS 4 .nf small more than 3: 15 big, 15 across nothing .fi .RE .PP The \fBwhen\fR belongs to the whole arm, and the \fB&&\fR to the whole \fBis\fR: either shape that is big enough takes them. .SH "WHAT THE COMPILER REFUSES" .SS "A name in one pattern only" .RS 4 .nf enum Expr { Add(int left, int right) Neg(int value) } void main() { var e = Expr.Neg(3) match e { Add(l, _) or Neg(v) => print(v) } } .fi .RE .PP .RS 4 .nf error: `l` is named in the first pattern of this `or` and not in this one every pattern of an `or` names the same values; write `_` in each for a value that is not needed --> example.sg:9:22 9 | Add(l, _) or Neg(v) => print(v) | ^^^^^^ .fi .RE .SS "A name of two types" .RS 4 .nf enum Item { Count(int n) Name(string s) } void main() { var item = Item.Count(3) match read item { Count(x) or Name(x) => print(x) } } .fi .RE .PP .RS 4 .nf error: `x` is a `read int` in the first pattern of this `or` and a `read string` here a name of an `or` has one type, whichever pattern took the value --> example.sg:9:26 9 | Count(x) or Name(x) => print(x) | ^ .fi .RE .SS "| between patterns" .RS 4 .nf void main() { var n = 3 match n { 1 | 2 => print("small") _ => print("big") } } .fi .RE .PP .RS 4 .nf error: patterns are joined with `or`, not `|` `|` and `||` are operators on values; a pattern is not a value: `'+' or '-'` --> example.sg:4:11 4 | 1 | 2 => print("small") | ^ hint: `or` .fi .RE .SS "A pattern that can never match" .RS 4 .nf void main() { var n = 3 match n { 1..=5 => print("small") 3 or 9 => print("three or nine") _ => print("big") } } .fi .RE .PP .RS 4 .nf error: this pattern can never match: the arms above and the patterns before it in the `or` take every value it matches --> example.sg:5:9 5 | 3 or 9 => print("three or nine") | ^ .fi .RE .PP \fB1..=5\fR above takes 3 already. \fB9 => ...\fR alone says what is meant. .SS "or between two bool values" .RS 4 .nf void main() { var ready = true var forced = false if ready or forced { print("go") } } .fi .RE .PP .RS 4 .nf error: expected `{`, found `or` `or` joins patterns, after `is` and in an arm of a `match`; two conditions are joined with `||` --> example.sg:4:14 4 | if ready or forced { | ^^ hint: `if ready || forced` .fi .RE .PP \fBor\fR joins patterns only; two conditions are joined with \fB||\fR: \fBif ready || forced\fR\&. .SH "SEE ALSO" .BR sg-match (7), .BR sg-is (7), .BR sg-when (7), .BR sg-not (7), .BR sg-null (7) .PP https://sg\-lang.org/reference/or.html