.\" Made by website/plain/build.mjs from website/words/bool.md; change that file. .TH SG-BOOL 7 2026-11-28 "SG" "SG reference" .SH NAME bool \- true or false; the only type a condition can be .SH SYNOPSIS .TP \fBtrue, false\fR the two values .TP \fBa && b, a || b\fR both, either; the right side runs only when it is needed .TP \fB!a\fR the other value .TP \fBa == b, a != b\fR the same, not the same (!= is an exclusive or) .TP \fBx < y, x is Circle(r), xs.contains(v)\fR a comparison, a test, a method: each gives a bool .TP \fBc ? a : b\fR a value picked by a condition .TP \fBmatch flag { true => ... false => ... }\fR an arm for each value .SH DESCRIPTION A \fBbool\fR is \fBtrue\fR or \fBfalse\fR, and takes one byte; in compiled code it is C's \fBbool\fR\&. It is what a comparison, an \fBis\fR and a test such as \fBcontains\fR give, and what an \fBif\fR, a \fBwhile\fR, an \fBassert\fR and \fBc ? a : b\fR take. A condition must be a \fBbool\fR: a number, a text or a \fBT?\fR is never one on its own, and is compared or tested instead (\fBcount != 0\fR, \fBname is not null\fR). .PP \fB&&\fR and \fB||\fR stop as soon as the answer is known: the right side of \fB&&\fR runs only when the left is \fBtrue\fR, that of \fB||\fR only when it is \fBfalse\fR\&. So \fBi < xs.length() && xs[i] > 0\fR never reads past the end. \fB!\fR turns a \fBbool\fR around. \fB&\fR, \fB|\fR, \fB^\fR and \fB~\fR are the operators of the bits of an integer, not of a \fBbool\fR\&. .PP A \fBbool\fR is not a number. It has no order, \fBtrue == 1\fR is refused, and \fB(int)flag\fR too: \fBflag ? 1 : 0\fR says which number each value is. It is \fBEquatable\fR and \fBHashable\fR, so it can be compared with \fB==\fR and kept in a \fBHashSet\fR\&. It prints as \fBtrue\fR or \fBfalse\fR\&. .PP A \fBmatch\fR on a \fBbool\fR has an arm for \fBtrue\fR and one for \fBfalse\fR, or a \fB_\fR\&. A \fBbool\fR can be a const, a field, an item of a list or an array: \fBbool[8](false)\fR\&. .SH EXAMPLES .SS "Values and operators" .RS 4 .nf void main() { var raining = true bool cold = false print($"{raining && cold} {raining || cold} {!cold}") print($"{raining == cold} {raining != cold}") var warm = !cold print(warm) } .fi .RE .PP .RS 4 .nf false true true false true true .fi .RE .PP \fBraining\fR is a \fBbool\fR from its value, \fBcold\fR from its type written. .SS "A condition with a name" .RS 4 .nf bool isLeap(int year) { return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) } void main() { var years = List { 1900, 2000, 2024, 2026 } for year in years { var leap = isLeap(year) print($"{year}: {leap}, {(leap ? 366 : 365)} days") } } .fi .RE .PP .RS 4 .nf 1900: false, 365 days 2000: true, 366 days 2024: true, 366 days 2026: false, 365 days .fi .RE .PP A condition kept in a variable, or given back by a function, reads as what it means. \fB&&\fR binds tighter than \fB||\fR, so the parentheses are needed. \fB?:\fR goes in parentheses inside \fB{ }\fR of \fB$"..."\fR, where \fB:\fR starts a format. .SS "Only what is needed runs" .RS 4 .nf bool asked(string name, bool answer) { print($" asked {name}") return answer } void main() { var items = List { 4, 8 } nint i = 5 if i < items.length() && items[i] > 0 { print("positive") } else { print($"no item {i}") } print(asked("a", false) && asked("b", true)) print(asked("c", true) || asked("d", true)) } .fi .RE .PP .RS 4 .nf no item 5 asked a false asked c true .fi .RE .PP \fBitems[5]\fR is never read, since the left side is already \fBfalse\fR\&. \fBb\fR and \fBd\fR are never asked. .SS "A field of a struct" .RS 4 .nf struct Job { string title bool done } void main() { var jobs = List { Job { title = "write", done = true }, Job { title = "test", done = false }, Job { title = "ship", done = false }, } jobs[1].done = true nint finished = 0 for job in read jobs { print($"[{(job.done ? "x" : " ")}] {job.title}") finished += job.done ? 1 : 0 } var allDone = finished == jobs.length() print($"{finished} of {jobs.length()} done, all done: {allDone}") } .fi .RE .PP .RS 4 .nf [x] write [x] test [ ] ship 2 of 3 done, all done: false .fi .RE .PP \fBjob.done ? 1 : 0\fR is how a \fBbool\fR is counted: it does not become a number on its own. .SS "From is, and in a match" .RS 4 .nf enum Reply { Yes No Later(int days) } string word(bool on) { return match on { true => "on" false => "off" } } void main() { var reply = Reply.Later(3) bool waiting = reply is Later(_) bool soon = reply is Later(d) && d < 7 print($"{waiting} {soon}") print($"light {word(waiting)}") var votes = List { true, false, true } print($"{votes} {votes.contains(false)}") } .fi .RE .PP .RS 4 .nf true true light on [true, false, true] true .fi .RE .PP \fBis\fR gives a \fBbool\fR that can be kept, and \fBd\fR is seen on the right of \fB&&\fR\&. The \fBmatch\fR needs both arms, and has them. .SH "WHAT THE COMPILER REFUSES" .SS "A number as a condition" .RS 4 .nf void main() { var queue = List { 3, 1 } while queue.length() { print(queue.pop()) } } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `nint` --> example.sg:3:11 3 | while queue.length() { | ^^^^^^^^^^^^^^ hint: a number is not a condition; compare it: `queue.length() != 0` .fi .RE .SS "A T? as a condition" .RS 4 .nf void main() { string? name = null if name { print(name) } } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `string?` --> example.sg:3:8 3 | if name { | ^^^^ .fi .RE .PP A \fBT?\fR is tested with [\fBis\fR](is.md): \fBif name is not null\fR, and in the block \fBname\fR is a \fBstring\fR\&. .SS "A bool as a number" .RS 4 .nf void main() { bool done = true int score = (int)done print(score) } .fi .RE .PP .RS 4 .nf error: a `bool` value cannot be converted to `int` --> example.sg:3:17 3 | int score = (int)done | ^^^^^^^^^ hint: a `bool` is not a number: `done ? 1 : 0` .fi .RE .PP \fBdone ? 1 : 0\fR says which number each value is. The other way, \fB(bool)n\fR is refused too, with the hint \fBn != 0\fR\&. .SS "A bool compared with a number" .RS 4 .nf void main() { bool done = true if done == 1 { print("done") } } .fi .RE .PP .RS 4 .nf error: operator `==` is not defined for `bool` and `int` --> example.sg:3:8 3 | if done == 1 { | ^^^^^^^^^ hint: a `bool` is a test by itself: `done` .fi .RE .PP \fBif done\fR is the whole test. .SS "& on two bools" .RS 4 .nf void main() { bool cold = true bool wet = false print(cold & wet) } .fi .RE .PP .RS 4 .nf error: operator `&` is not defined for `bool` and `bool` --> example.sg:4:11 4 | print(cold & wet) | ^^^^^^^^^^ hint: for two `bool`s write `&&` .fi .RE .PP \fB|\fR is refused the same way, with \fB||\fR as the hint, and \fB^\fR with \fB!=\fR\&. \fBready &= ok\fR is refused with the hint \fBready = ready && ok\fR, and there is no \fB&&=\fR or \fB||=\fR: the assignment is written out. .SS "Two comparisons in a row" .RS 4 .nf void main() { int age = 30 if 18 <= age < 65 { print("working age") } } .fi .RE .PP .RS 4 .nf error: operator `<` is not defined for `bool` and `int` --> example.sg:3:8 3 | if 18 <= age < 65 { | ^^^^^^^^^^^^^^ hint: compare twice: `18 <= age && age < 65` .fi .RE .PP \fB18 <= age\fR is a \fBbool\fR, which is then compared with 65. Two tests are joined with \fB&&\fR: \fB18 <= age && age < 65\fR\&. .SS "A match with one arm" .RS 4 .nf string label(bool on) { return match on { true => "on" } } void main() { print(label(true)) } .fi .RE .PP .RS 4 .nf error: this `match` does not handle `false` --> example.sg:2:18 2 | return match on { | ^^ hint: add an arm for it, or `_ => ...` for everything else .fi .RE .SS "True with a capital" .RS 4 .nf void main() { bool ready = True print(ready) } .fi .RE .PP .RS 4 .nf error: there is no variable named `True` --> example.sg:2:18 2 | bool ready = True | ^^^^ hint: a `bool` is written in lower case: `true` .fi .RE .PP The two values are written \fBtrue\fR and \fBfalse\fR\&. .SS "bool.parse" .RS 4 .nf void main() { var answer = "true" print(bool.parse(answer)) } .fi .RE .PP .RS 4 .nf error: `bool` has no function `parse` --> example.sg:3:16 3 | print(bool.parse(answer)) | ^^^^^ hint: compare the text: `answer == "true"` .fi .RE .PP A comparison says exactly which texts count as \fBtrue\fR\&. .SH "SEE ALSO" .BR sg-true (7), .BR sg-false (7), .BR sg-if (7), .BR sg-while (7), .BR sg-is (7), .BR sg-match (7) .PP https://sg\-lang.org/reference/bool.html