.\" Made by website/plain/build.mjs from website/words/conditional.md; change that file. .TH SG-CONDITIONAL 7 2026-11-28 "SG" "SG reference" .SH NAME c ? a : b \- a value picked by a condition: a when c is true, b when it is false; only that arm runs .SH SYNOPSIS .TP \fBc ? a : b\fR a when c is true, b when it is false .TP \fBc ? 1 : 2.5, c ? name : null\fR an arm that is only a number or null takes the type of the other one; null makes it a T? .TP \fBa ? 1 : b ? 2 : 3\fR a chain: it groups to the right and reads as else if .TP \fBx is Circle(r) ? r : 0, p is not null ? p.x : 0\fR the first arm sees the names of an is and what it narrows .TP \fBr is not Ok(v) ? \-1 : v\fR the second arm sees the names of an is not .TP \fBf ?? false ? a : b, x > 0 || y > 0 ? a : b\fR it binds more loosely than every other operator, ?? too .TP \fB$"{(c ? a : b)}"\fR inside { } of a formatted text it is put in parentheses .TP \fBc ? or c ? a : at the end of a line\fR the expression goes on in the next line .SH DESCRIPTION \fBc ? a : b\fR is a value. When \fBc\fR is \fBtrue\fR it is \fBa\fR, and when \fBc\fR is \fBfalse\fR it is \fBb\fR\&. Only the arm chosen is computed: the other one never runs, so an arm may hold a call, an index or a division that would be wrong on the other way. The condition must be a \fBbool\fR, as the condition of an \fBif\fR\&. .PP Both arms have one type, as the arms of a \fBmatch\fR\&. The first arm sets it, and the second must fit it the way a value fits a variable of that type: after a \fBlong\fR, an \fBint\fR arm is taken, but not a \fBlong\fR after an \fBint\fR\&. When the type is written where the result goes (\fBlong sum = c ? small : big\fR), both arms are checked against it. An arm that is only a number takes the type of the other arm, so \fBc ? 1 : 2.5\fR is a \fBdouble\fR, and \fBc ? 1 : 5000000000\fR a \fBlong\fR, as \fBvar big = 5000000000\fR is. An arm that is only \fBnull\fR does too, and makes the result a \fBT?\fR: \fBc ? "Pasha" : null\fR is a \fBstring?\fR, and \fBc ? null : 10\fR an \fBint?\fR\&. Two arms that are only \fBnull\fR need the type written where the result goes: \fBint? limit = c ? null : null\fR\&. .PP \fB?:\fR binds more loosely than any other operator, \fB??\fR, \fB&&\fR and \fB||\fR too: \fBverbose ?? false ? a : b\fR is \fB(verbose ?? false) ? a : b\fR, and \fB1 + (c ? 10 : 20)\fR needs its parentheses. It groups to the right, so \fBn > 0 ? 1 : n < 0 ? \-1 : 0\fR is \fBn > 0 ? 1 : (n < 0 ? \-1 : 0)\fR, which reads as an \fBif\fR with \fBelse if\fR\&. A line that ends with \fB?\fR or \fB:\fR goes on in the next one; a line cannot start with either. Inside \fB{ }\fR of \fB$"..."\fR a \fB:\fR starts the format, so a \fB?:\fR there is written in parentheses: \fB$"{(c ? a : b)}"\fR\&. .PP The arms see what the condition tells, as the blocks of an \fBif\fR do. After \fBx is Circle(r)\fR, the first arm has \fBr\fR; after \fBname is not null\fR, \fBname\fR is a \fBstring\fR in the first arm. The second arm sees the names of an \fBis not\fR and what a failed test narrows. A name of an \fBis\fR is never seen in the other arm. .PP An owned value in an arm is moved, as \fBvar y = x\fR would move it. The compiler does not know which arm runs, so a name moved in either arm may have been moved, and can no longer be used after the line. \fBread x\fR in an arm looks without moving; while the result is used, the names of both arms stay lent, whichever arm ran, and so does what an \fBis\fR in the condition looked into (after \fBvar t = r is Ok(s) ? s : "none"\fR, \fBr\fR cannot be moved while \fBt\fR is used). .PP \fB?:\fR is not a statement: its value must be used. An arm that gives nothing (a call of a \fBvoid\fR function) is an error, and an arm cannot \fBreturn\fR, \fBbreak\fR or \fBcontinue\fR\&. To do one thing or the other, write an \fBif\fR; to leave when a value is missing, \fB?? return\fR\&. An arm may stop the program with \fBpanic(...)\fR, and it then gives no value to what comes after. .PP It costs nothing at run time: compiled, it is written as the \fBif\fR it is, and the interpreter and compiled code compute the same things in the same order. .SH EXAMPLES .SS "A value picked by a condition" .RS 4 .nf int larger(int a, int b) { return a > b ? a : b } void main() { var count = 1 var word = count == 1 ? "file" : "files" print(word) print(larger(3, 8)) var temperature = -4 print(temperature < 0 ? "freezing" : "above zero") } .fi .RE .PP .RS 4 .nf file 8 freezing .fi .RE .PP \fB?:\fR goes wherever a value goes: in a declaration, after \fBreturn\fR, as an argument. .SS "Only the arm chosen runs" .RS 4 .nf int loud(int n) { print($"computing {n}") return n } void main() { var ready = true var x = ready ? loud(1) : loud(2) print(x) var scores = List { 7, 9 } nint i = 5 print(i < scores.length() ? scores[i] : -1) var divisor = 0 print(divisor != 0 ? 100 / divisor : 0) } .fi .RE .PP .RS 4 .nf computing 1 1 -1 0 .fi .RE .PP \fBloud(2)\fR is never called. \fBscores[5]\fR is never read and \fB100 / 0\fR never computed, so neither stops the program. .SS "The type of the two arms" .RS 4 .nf void main() { var exact = false var half = exact ? 1 : 2.5 print(half / 2) int small = 4 long big = 5000000000 var either = exact ? big : small print(either) long sum = exact ? small : big print(sum) string? nickname = exact ? "Pasha" : null print(nickname ?? "no nickname") int? limit = exact ? null : 10 print(limit ?? 0) } .fi .RE .PP .RS 4 .nf 1.25 4 5000000000 no nickname 10 .fi .RE .PP \fB1\fR takes the type of \fB2.5\fR, so \fBhalf\fR is a \fBdouble\fR\&. \fBeither\fR is a \fBlong\fR, set by its first arm; with the \fBint\fR first, the type is written where the result goes, as in \fBlong sum\fR\&. An arm that is \fBnull\fR makes a \fBT?\fR\&. .SS "Chains, other operators and formatted text" .RS 4 .nf int sign(int n) { return n > 0 ? 1 : n < 0 ? -1 : 0 } string sizeOf(int bytes) { return bytes < 1024 ? "small" : bytes < 1024 * 1024 ? "medium" : "large" } void main() { print(sign(-8)) print(sizeOf(4096)) var age = 70 print(age < 12 || age >= 65 ? "reduced price" : "full price") print(1 + (age > 18 ? 10 : 20)) bool? verbose = null print(verbose ?? false ? "loud" : "quiet") var count = 3 print($"{count} {(count == 1 ? "file" : "files")}") print($"{(count > 2 ? 255 : 0):X4}") } .fi .RE .PP .RS 4 .nf -1 medium reduced price 11 quiet 3 files 00FF .fi .RE .PP A chain is tried from the left, and the first condition that holds picks its value. Each line of \fBsizeOf\fR ends with \fB:\fR, so the \fBreturn\fR goes on. \fB||\fR and \fB??\fR are computed before the \fB?\fR, and \fB+\fR needs the parentheses. In the last line the parentheses end the \fB?:\fR, and \fB:X4\fR after them is the format. .SS "With is" .RS 4 .nf enum Shape { Circle(double r) Square(double side) } double area(read Shape shape) { return shape is Circle(r) ? 3.0 * r * r : shape is Square(side) ? side * side : 0 } int parsedOr(read string text) { var parsed = int.parse(text) return parsed is not Ok(n) ? -1 : n } void main() { print(area(read Shape.Circle(2.0))) print(area(read Shape.Square(1.5))) print(parsedOr("42")) print(parsedOr("4x")) string? name = null print(name is not null ? name.length() : 0) name = "Ada" print(name is null ? 0 : name.length()) } .fi .RE .PP .RS 4 .nf 12 2.25 42 -1 0 3 .fi .RE .PP \fBr\fR is seen in the first arm of its \fB?:\fR, \fBside\fR in the first arm of the inner one, and \fBn\fR of an \fBis not\fR in the second arm. Where \fBname is not null\fR holds, \fBname\fR is a \fBstring\fR and has a \fBlength()\fR\&. .SS "Owned values and borrows" .RS 4 .nf read string longer(read string a, read string b) from a, b { return a.length() >= b.length() ? a : b } void main() { var cat = "Pasha".clone() var other = "Tekir".clone() var shown = cat.length() > 9 ? read cat : read other print(shown) print(cat) print(longer(read cat, read other)) var first = true var kept = first ? cat : other print(kept) } .fi .RE .PP .RS 4 .nf Tekir Pasha Pasha Pasha .fi .RE .PP \fBread cat\fR and \fBread other\fR only look, so both names are used again. \fBlonger\fR gives a borrow of one of its parameters, so it says \fBfrom a, b\fR\&. The last \fB?:\fR moves: \fBkept\fR owns the text of \fBcat\fR, and after it both \fBcat\fR and \fBother\fR count as moved. .SH "WHAT STOPS THE PROGRAM" .SS "A panic in an arm" .RS 4 .nf void main() { var stock = List { 3, 0 } for left in stock { var each = left > 0 ? 12 / left : panic("nothing left") print(each) } } .fi .RE .PP .RS 4 .nf 4 panic: nothing left --> example.sg:4:43 4 | var each = left > 0 ? 12 / left : panic("nothing left") | ^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP \fBpanic(...)\fR gives no value, so the other arm sets the type: \fBeach\fR is an \fBint\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "?: as a statement" .RS 4 .nf void start() { print("start") } void stop() { print("stop") } void main() { var ready = true ready ? start() : stop() } .fi .RE .PP .RS 4 .nf error: `?:` gives a value; it is not a statement --> example.sg:11:5 11 | ready ? start() : stop() | ^^^^^^^^^^^^^^^^^^^^^^^^ hint: to do one thing or the other, write an `if`: `if ready { ... } else { ... }` .fi .RE .SS "An arm that gives nothing" .RS 4 .nf void log(read string text) { print(text) } void main() { var count = 3 var done = count > 2 ? log("many") : log("few") } .fi .RE .PP .RS 4 .nf error: this arm of `?:` gives no value --> example.sg:7:28 7 | var done = count > 2 ? log("many") : log("few") | ^^^^^^^^^^^ hint: to do one thing or the other, write an `if`: `if count > 2 { ... } else { ... }` .fi .RE .SS "return in an arm" .RS 4 .nf int firstOr(read List numbers) { var first = numbers.length() > 0 ? numbers[0] : return -1 return first } void main() { print(firstOr(read List { 4 })) } .fi .RE .PP .RS 4 .nf error: an arm of `?:` gives a value; it cannot leave the function --> example.sg:2:53 2 | var first = numbers.length() > 0 ? numbers[0] : return -1 | ^^^^^^ hint: leave with an `if`, or with `?? return` on a value that may be `null` .fi .RE .PP \fBnumbers.get(0) ?? return \-1\fR is the line that leaves. .SS "A number as the condition" .RS 4 .nf void main() { var count = 3 print(count ? "some" : "none") } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `int` --> example.sg:3:11 3 | print(count ? "some" : "none") | ^^^^^ hint: a number is not a condition; compare it: `count != 0` .fi .RE .SS "Arms of two types" .RS 4 .nf void main() { int small = 4 long big = 5000000000 var exact = true var either = exact ? small : big print(either) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `long` both arms of `?:` give one type, and the other arm gives `int` --> example.sg:5:34 5 | var either = exact ? small : big | ^^^ hint: the first arm gives the type of both: widen it, `exact ? (long)small : big`, or give the result the type `long` where it goes .fi .RE .PP The first arm made it an \fBint\fR, and \fB?:\fR does not widen it afterwards, as the first arm of a \fBmatch\fR gives the type of the others. \fBlong either = exact ? small : big\fR gives both arms the type \fBlong\fR\&. .SS "null and null, with no type written" .RS 4 .nf void main() { var exact = true var limit = exact ? null : null print(limit ?? 0) } .fi .RE .PP .RS 4 .nf error: the type of this `?:` cannot be found from `null` alone --> example.sg:3:25 3 | var limit = exact ? null : null | ^^^^ .fi .RE .PP Each arm waits for the type of the other. \fBint? limit = exact ? null : null\fR writes it; \fBexact ? null : 10\fR needs nothing written, and is an \fBint?\fR\&. .SS "Inside { } without parentheses" .RS 4 .nf void main() { var count = 3 print($"{count} {count == 1 ? "file" : "files"}") } .fi .RE .PP .RS 4 .nf error: `c ? a : b` inside `{ }` is written in parentheses a `:` there starts the format, as in `{x:X}` --> example.sg:3:33 3 | print($"{count} {count == 1 ? "file" : "files"}") | ^^^^^^^^^^ hint: write `{(c ? a : b)}` .fi .RE .SS "A name of is in the second arm" .RS 4 .nf enum Shape { Circle(double r) Square(double side) } void main() { var shape = Shape.Square(2.0) var size = shape is Circle(r) ? r * 2.0 : r print(size) } .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:8:47 8 | var size = shape is Circle(r) ? r * 2.0 : r | ^ .fi .RE .SS "A name moved in the arm that did not run" .RS 4 .nf void main() { var cat = "Pasha".clone() var other = "Tekir".clone() var first = true var kept = first ? cat : other print(kept) print(other) } .fi .RE .PP .RS 4 .nf error: `other` may have been moved and can no longer be used --> example.sg:7:11 7 | print(other) | ^^^^^ it is moved here, on one of the ways to this line: --> example.sg:5:30 5 | var kept = first ? cat : other | ^^^^^ .fi .RE .PP The compiler does not know which arm runs. \fBfirst ? read cat : read other\fR looks without moving. .SS "A line that starts with ?" .RS 4 .nf void main() { var count = 3 var word = count == 1 ? "file" : "files" print(word) } .fi .RE .PP .RS 4 .nf error: a line cannot start with `?` --> example.sg:4:9 4 | ? "file" : "files" | ^ hint: the statement ended on the previous line; to continue it, put `?` at the end of that line .fi .RE .PP A line that starts with \fB:\fR gets the same message, with \fB:\fR\&. .SS "A T? as the condition" .RS 4 .nf void main() { string? name = null print(name ? "named" : "nameless") } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `string?` --> example.sg:3:11 3 | print(name ? "named" : "nameless") | ^^^^ hint: `name is not null` tells whether it holds a value; in a condition it also makes `name` a `string` where the test holds .fi .RE .SH "SEE ALSO" .BR sg-if (7), .BR sg-else (7), .BR sg-bool (7), .BR sg-is (7), .BR sg-null-coalescing (7), .BR sg-interpolation (7), .BR sg-null (7), .BR sg-nullable (7), .BR sg-match (7) .PP https://sg\-lang.org/reference/conditional.html