.\" Made by website/plain/build.mjs from website/words/not.md; change that file. .TH SG-NOT 7 2026-11-28 "SG" "SG reference" .SH NAME not \- turns the test of is around: x is not P, whose names are seen where the test failed .SH SYNOPSIS .TP \fBx is not null\fR true when a T? holds a value; where it holds, x is a T .TP \fBx is not Empty\fR true when x is any other variant .TP \fBx is not A or B\fR true when neither A nor B fits .TP \fBx is not Ok(n) || n < 1\fR the names are seen on the right of || .TP \fBif x is not Ok(n) { return }, then n\fR and after an if whose block always leaves .TP \fBif x is not Rect(w, h) { ... } else { ... }\fR and in else .TP \fBx is not Rect(w, _) ? 0 : w\fR and in the second arm of ?: .TP \fBwhile x is not Number(n) { ... }, then n\fR and after a while that only its test ends .TP \fBref x is not Circle(r)\fR the names borrow to change .SH DESCRIPTION \fBnot\fR is written in one place: right after \fBis\fR\&. \fBx is not P\fR is the test of \fBx is P\fR the other way round. It is true when \fBx\fR is another variant than \fBP\fR, or, with \fBnull\fR, when the \fBT?\fR holds a value. Everything else is as for \fBis\fR: \fBP\fR is a variant, with the patterns of \fBmatch\fR inside its parentheses, or \fBnull\fR; \fBx is not A or B\fR tests the whole \fBor\fR, and is true when neither \fBA\fR nor \fBB\fR fits. \fB!(x is P)\fR is the same test. .PP On a \fBT?\fR, where \fBx is not null\fR holds, \fBx\fR is a \fBT\fR: in the block of the \fBif\fR or \fBwhile\fR, on the right of \fB&&\fR, and in the first arm of \fB?:\fR\&. .PP The names of \fBx is not P\fR are seen where the test failed, since only there did \fBP\fR fit: on the right of \fB||\fR, in \fBelse\fR, in the second arm of \fB?:\fR, after an \fBif\fR whose block always leaves (\fBreturn\fR, \fBbreak\fR, \fBcontinue\fR, \fBpanic\fR, \fBexit\fR), and after a \fBwhile\fR that only its test ends. A name used where the test held is an error, and so is a \fBbreak\fR that leaves such a \fBwhile\fR\&. Like those of \fBis\fR, the names are \fBread\fR borrows of what \fBx\fR carries, and \fBref\fR borrows when \fBx\fR is a \fBref\fR borrow or the test is written \fBref x is not ...\fR\&. .PP \fBnot\fR is not an operator. A \fBbool\fR is turned around with \fB!\fR: \fB!ready\fR\&. .PP \fBnot\fR is a word only right after \fBis\fR\&. Elsewhere it is a name like any other: \fBint not = 4\fR is a variable, and in \fBx is not not\fR the second \fBnot\fR is read as the name of a variant. .SH EXAMPLES .SS "null" .RS 4 .nf void main() { var ages = List { 36, null, 45 } var total = 0 for age in ages { if age is not null { total += age } } print(total) int? missing = null print(missing is not null) } .fi .RE .PP .RS 4 .nf 81 false .fi .RE .PP In the block, \fBage\fR is an \fBint\fR, so it can be added. An \fBis not\fR that names nothing is a \fBbool\fR like any other, and can be printed. .SS "Another variant" .RS 4 .nf enum Light { Red Amber Green Off } void main() { var lights = List { Light.Red, Light.Off, Light.Green } for light in read lights { if light is not Off { print("lit") } if light is not Red or Amber { print("neither red nor amber") } } } .fi .RE .PP .RS 4 .nf lit neither red nor amber lit neither red nor amber .fi .RE .PP \fBis not Red or Amber\fR takes the whole \fBor\fR: it is true for \fBOff\fR and \fBGreen\fR\&. .SS "The names after an if that leaves" .RS 4 .nf void main() { var fields = List { "12", "x", "30" } var total = 0 for field in read fields { if int.parse(field) is not Ok(n) { print($"{field} is not a number") continue } total += n } print(total) } .fi .RE .PP .RS 4 .nf x is not a number 42 .fi .RE .PP The block ends with \fBcontinue\fR, so the code after the \fBif\fR runs only when the parse gave \fBOk\fR, and \fBn\fR is seen there. .SS "||, else and ?:" .RS 4 .nf enum Shape { Circle(int radius) Rect(int width, int height) } void describe(read Shape shape) { if shape is not Circle(r) || r > 10 { print("not a small circle") } if shape is not Rect(w, h) { print("round") } else { print($"{w} by {h}") } var width = shape is not Rect(w, _) ? 0 : w print($"width {width}") } void main() { describe(read Shape.Circle(2)) describe(read Shape.Rect(3, 4)) } .fi .RE .PP .RS 4 .nf round width 0 not a small circle 3 by 4 width 3 .fi .RE .PP \fBr\fR is seen on the right of \fB||\fR, where the shape is a circle. \fBw\fR and \fBh\fR are seen in \fBelse\fR, and \fBw\fR in the second arm of \fB?:\fR: in each, the test failed and \fBRect\fR fit. .SS "An or that names, and ref" .RS 4 .nf enum Shape : Formattable { Circle(int radius) Square(int side) Empty } void grow(ref Shape shape) { if shape is not Circle(n) or Square(n) { print("nothing to grow") return } n *= 2 } void main() { var shapes = List { Shape.Circle(3), Shape.Square(5), Shape.Empty } for shape in ref shapes { grow(ref shape) } print(shapes) } .fi .RE .PP .RS 4 .nf nothing to grow [Circle(6), Square(10), Empty] .fi .RE .PP Both patterns name \fBn\fR, an \fBint\fR, so after the \fBif\fR it is the radius or the side, whichever fit. \fBshape\fR is a \fBref\fR borrow, so \fBn\fR is one too, and writing it changes the shape in the list. .SS "The names after a while" .RS 4 .nf enum Token : Formattable { Number(int value) Word(string text) } Token tokenOf(read string text) { return match int.parse(text) { Ok(n) => Token.Number(n) Error(_) => Token.Word(text.clone()) } } void main() { var words = List { "let", "x", "7", "y" } nint at = 0 var token = tokenOf(words[at]) while token is not Number(n) { print($"skip {token}") at += 1 token = tokenOf(words[at]) } print($"the first number is {n}, at {at}") } .fi .RE .PP .RS 4 .nf skip Word("let") skip Word("x") the first number is 7, at 2 .fi .RE .PP The loop has no \fBbreak\fR: it ends only when its test fails, so after it \fBtoken\fR is a \fBNumber\fR and \fBn\fR is seen. The test is made on a variable, which the loop changes; a value with no name, made again in every round, could not be looked at after the loop. .SH "WHAT THE COMPILER REFUSES" .SS "A name where the test held" .RS 4 .nf enum Shape { Circle(int radius) Empty } void main() { var s = Shape.Circle(2) if s is not Circle(r) { print(r) } } .fi .RE .PP .RS 4 .nf error: nothing here can use the names in this pattern the names of `is not` are seen where its test is sure to have failed: on the right of `||`, in the second arm of its `?:`, in `else`, after an `if` whose block leaves, and after a `while` that only its test ends this `if` has no `else`, and the code after it also runs when the block has run --> example.sg:8:17 8 | if s is not Circle(r) { | ^^^^^^^^^ hint: end the block with `return`, or use `match` .fi .RE .PP In the block the shape is not a circle, so there is no radius. \fBif s is Circle(r)\fR says what is meant. .SS "A name on the right of &&" .RS 4 .nf enum Shape { Circle(int radius) Empty } void main() { var s = Shape.Circle(2) if s is not Circle(r) && r > 1 { print("a big circle, or none") } } .fi .RE .PP .RS 4 .nf error: `is not` holds when the value is another variant, so nothing here can use the names the names of `is not` are seen where its test is sure to have failed: on the right of `||`, in the second arm of its `?:`, in `else`, after an `if` whose block leaves, and after a `while` that only its test ends --> example.sg:8:17 8 | if s is not Circle(r) && r > 1 { | ^^^^^^^^^ hint: write `_` for each value, or test `is` without `not` .fi .RE .PP The right of \fB&&\fR runs only where the left held. With \fB||\fR the names are seen: \fBs is not Circle(r) || r > 1\fR\&. .SS "A while that a break leaves" .RS 4 .nf enum Token { Number(int value) Word(string text) } void main() { var token = Token.Word("let".clone()) var tries = 0 while token is not Number(n) { tries += 1 if tries == 3 { break } token = Token.Number(tries) } print(n) } .fi .RE .PP .RS 4 .nf error: nothing here can use the names in this pattern after a `while`, the names of `is not` are seen only when the test is what ends the loop --> example.sg:9:24 9 | while token is not Number(n) { | ^^^^^^^^^ this `break` leaves the loop while the test may still hold: --> example.sg:12:13 12 | break | ^^^^^ hint: test the value again after the loop, or use `match` .fi .RE .PP After the \fBbreak\fR, \fBtoken\fR may still be a \fBWord\fR\&. .SS "not in an arm of a match" .RS 4 .nf enum Light { Red Green Off } void main() { var light = Light.Green match light { not Off => print("lit") Off => print("dark") } } .fi .RE .PP .RS 4 .nf error: expected `=>`, found `Off` `not` is written only right after `is`, as in `is not null`; a pattern of a `match` has no `not` --> example.sg:10:13 10 | not Off => print("lit") | ^^^ hint: an arm for `Off` first, then one for the rest: `Off => ...` and `_ => ...` .fi .RE .PP \fBnot\fR is written only after \fBis\fR\&. In a \fBmatch\fR, the arm of \fBOff\fR goes first, and \fB_ => ...\fR takes the rest. .SS "not on a bool" .RS 4 .nf void main() { var ready = false if not ready { print("wait") } } .fi .RE .PP .RS 4 .nf error: expected `{`, found `ready` `not` is written only right after `is`, as in `is not null`; a condition is turned around with `!` --> example.sg:3:12 3 | if not ready { | ^^^^^ hint: `if !ready` .fi .RE .PP \fBnot\fR turns a test of \fBis\fR around, not a value. A \fBbool\fR is turned around with \fB!\fR: \fBif !ready\fR\&. .SH "SEE ALSO" .BR sg-is (7), .BR sg-null (7), .BR sg-or (7), .BR sg-match (7), .BR sg-bool (7), .BR sg-nullable (7) .PP https://sg\-lang.org/reference/not.html