.\" Made by website/plain/build.mjs from website/words/is.md; change that file. .TH SG-IS 7 2026-11-28 "SG" "SG reference" .SH NAME is \- tests whether a value is one variant, or null, and names what it carries .SH SYNOPSIS .TP \fBx is Circle(r)\fR true when x is that variant; r borrows what it carries .TP \fBx is Rect(w, _)\fR _ skips a value; each value is named or skipped .TP \fBx is Empty\fR a variant that carries nothing .TP \fBx is Ok(n), x is Error(e)\fR the same on a Result .TP \fBx is null\fR true when a T? holds nothing .TP \fBx is not P\fR the same test, the other way round; not is written only here .TP \fBx is Circle(r) && r > 2\fR the names are seen on the right of && and in the block .TP \fBref x is Circle(r)\fR the names borrow to change .TP \fBx is Number(1..=9 or 40), x is Word("let")\fR a pattern inside the parentheses, as in match .SH DESCRIPTION \fBis\fR asks whether a value has one shape: a variant of an enum, \fBOk\fR or \fBError\fR of a \fBResult\fR, or \fBnull\fR\&. It gives a \fBbool\fR, so it goes where a condition goes: an \fBif\fR, a \fBwhile\fR, \fB&&\fR and \fB||\fR, the condition of \fB?:\fR\&. An \fBis\fR that names values goes where its names can be seen: on the left of \fB&&\fR, and an \fBis not\fR on the left of \fB||\fR\&. \fBx is not P\fR is true when \fBx is P\fR is false. .PP The names in the pattern are seen only where the test is sure to have held: in the block of the \fBif\fR or \fBwhile\fR, on the right of \fB&&\fR, and in the first arm of \fB?:\fR\&. For \fBis not\fR it is the other way round: the names are seen where the test failed, on the right of \fB||\fR, in \fBelse\fR, and after an \fBif\fR whose block always leaves (\fBreturn\fR, \fBbreak\fR, \fBcontinue\fR). A name used anywhere else is an error. .PP \fBis\fR only looks. It never moves \fBx\fR, and its names are \fBread\fR borrows of what \fBx\fR carries. They are \fBref\fR borrows when \fBx\fR is a \fBref\fR borrow, or when the test is written \fBref x is ...\fR\&. To take the values out and own them, write a \fBmatch\fR\&. .PP On a \fBT?\fR, \fBx is not null\fR also narrows: where it holds, \fBx\fR is a \fBT\fR\&. Unlike a \fBmatch\fR, \fBis\fR tests one pattern and does not ask for every case. At its top it takes a variant or \fBnull\fR, never a number or a range; inside the parentheses go the patterns of \fBmatch\fR\&. .SH EXAMPLES .SS "A variant in an if" .RS 4 .nf enum Shape { Circle(int radius) Rect(int width, int height) Empty } void describe(read Shape shape) { if shape is Circle(r) { print($"a circle of radius {r}") } if shape is Rect(w, _) && w > 10 { print($"a wide rectangle, {w} across") } if shape is Empty { print("nothing") } } void main() { describe(read Shape.Circle(3)) describe(read Shape.Rect(12, 2)) describe(read Shape.Rect(4, 2)) describe(read Shape.Empty) } .fi .RE .PP .RS 4 .nf a circle of radius 3 a wide rectangle, 12 across nothing .fi .RE .PP \fBr\fR is the radius the circle carries. \fBw\fR is seen on the right of \fB&&\fR too, so the narrow rectangle prints nothing. .SS "null" .RS 4 .nf void greet(read string? name) { if name is null { print("hello, stranger") return } print($"hello, {name}, {name.length()} letters") } void main() { greet(null) greet("Ada") string? nickname = null print(nickname is not null) } .fi .RE .PP .RS 4 .nf hello, stranger hello, Ada, 3 letters false .fi .RE .PP After the \fBif\fR, which returns, \fBname\fR is a \fBread string\fR and has a \fBlength()\fR\&. An \fBis\fR that names nothing is a \fBbool\fR like any other, and can be printed. .SS "is not, and the names after the if" .RS 4 .nf int portOf(read string text) { if int.parse(text) is not Ok(port) || port < 1 || port > 65535 { return 80 } return port } void main() { print(portOf("8080")) print(portOf("http")) print(portOf("70000")) } .fi .RE .PP .RS 4 .nf 8080 80 80 .fi .RE .PP \fBport\fR is seen on the right of \fB||\fR, where the parse did not fail, and after the \fBif\fR, whose block returns. The \fBResult\fR that \fBint.parse\fR gives has no name; it stays until the end of the function. .SS "Patterns inside the parentheses" .RS 4 .nf enum Token : Formattable { Number(int value) Word(string text) Plus } void main() { var tokens = List { Token.Number(2), Token.Plus, Token.Word("x".clone()), Token.Number(40) } for token in read tokens { if token is Number(1..=9 or 40) { print("a small number, or forty") } if token is Word("x") { print("the word x") } if token is not Plus { print(token) } } } .fi .RE .PP .RS 4 .nf a small number, or forty Number(2) the word x Word("x") a small number, or forty Number(40) .fi .RE .PP Ranges, \fBor\fR and texts test the value inside the variant, as they do in a \fBmatch\fR\&. .SS "In a while" .RS 4 .nf void main() { var fields = List { "12", "30", "x7", "5" } nint i = 0 var total = 0 while i < fields.length() && int.parse(read fields[i]) is Ok(n) { total += n i += 1 } print($"{total} from the first {i} fields") } .fi .RE .PP .RS 4 .nf 42 from the first 2 fields .fi .RE .PP The loop goes on while the next field is a number, and \fBn\fR is that number in each round. It stops at \fB"x7"\fR\&. .SS "Changing what a variant carries" .RS 4 .nf enum Shape : Formattable { Circle(int radius) Rect(int width, int height) } void grow(ref Shape shape) { if shape is Circle(r) { r += 1 } } void main() { var shape = Shape.Rect(3, 4) if ref shape is Rect(w, _) { w = 10 } print(shape) var round = Shape.Circle(1) grow(ref round) print(round) } .fi .RE .PP .RS 4 .nf Rect(10, 4) Circle(2) .fi .RE .PP \fBref shape is\fR makes \fBw\fR a \fBref int\fR, so writing it changes \fBshape\fR\&. In \fBgrow\fR, \fBshape\fR is a \fBref\fR borrow already, and \fBr\fR is one too. .SS "Looking, then taking with match" .RS 4 .nf enum Message { Text(string body) Quit } void keep(string body) { print($"kept {body}") } void main() { var m = Message.Text("hello".clone()) if m is Text(body) { print(body.length()) } match m { Text(body) => keep(body) Quit => print("quit") } } .fi .RE .PP .RS 4 .nf 5 kept hello .fi .RE .PP The \fBis\fR only looks, so \fBm\fR still owns its text after it. The \fBmatch\fR takes \fBm\fR, and there \fBbody\fR owns the text and can be given away. .SH "WHAT THE COMPILER REFUSES" .SS "A variant without its values" .RS 4 .nf enum Shape { Circle(int radius) Rect(int width, int height) } void main() { var shape = Shape.Rect(3, 4) if shape is Rect { print("a rectangle") } } .fi .RE .PP .RS 4 .nf error: `Rect` carries 2 value(s); the pattern must name or skip each --> example.sg:8:17 8 | if shape is Rect { | ^^^^ hint: `Rect(width, height)`, or `Rect(_, _)` to skip them .fi .RE .SS "A name where the test may have failed" .RS 4 .nf enum Shape { Circle(int radius) Empty } void main() { var shape = Shape.Circle(2) if shape is Circle(r) { print("round") } else { print(r) } } .fi .RE .PP .RS 4 .nf error: there is no variable named `r` the names of `is` are seen where its test is sure to have held: on the right of `&&`, in the block of its `if` or `while`, in the first arm of its `?:`, and after an `if` whose `else` leaves --> example.sg:11:15 11 | print(r) | ^ `r` is named by this `is`: --> example.sg:8:24 8 | if shape is Circle(r) { | ^ hint: use the value where the name is seen, or take it out with `match` .fi .RE .SS "Giving away what is only borrows" .RS 4 .nf enum Message { Text(string body) Quit } void keep(string body) { print(body) } void main() { var m = Message.Text("hello".clone()) if m is Text(body) { keep(body) } } .fi .RE .PP .RS 4 .nf error: expected `string`, found `read string` --> example.sg:13:14 13 | keep(body) | ^^^^ hint: `read string` only borrows the text; to own a copy write `body.clone()` .fi .RE .PP \fBbody\fR borrows from \fBm\fR\&. A \fBmatch m\fR takes the text out, as in the last example. .SS "A number in place of a variant" .RS 4 .nf void main() { var port = 8080 if port is 1..=65535 { print("a port") } } .fi .RE .PP .RS 4 .nf error: expected `null` or a variant name, found `1` `is` tests for `null` or for one variant of an enum --> example.sg:3:16 3 | if port is 1..=65535 { | ^ .fi .RE .PP A number is compared with \fB<\fR and \fB>\fR, or sorted by a \fBmatch\fR, where \fB1..=65535\fR is a pattern. .SH "SEE ALSO" .BR sg-match (7), .BR sg-not (7), .BR sg-null (7), .BR sg-nullable (7), .BR sg-or (7), .BR sg-ref (7) .PP https://sg\-lang.org/reference/is.html