.\" Made by website/plain/build.mjs from website/words/null-coalescing.md; change that file. .TH SG-NULL-COALESCING 7 2026-11-28 "SG" "SG reference" .SH NAME x ?? y \- the value of x, or y when x is null or an error .SH SYNOPSIS .TP \fBx ?? y\fR on a T?: the value of x, or y when x is null .TP \fBr ?? y\fR on a Result: the value of r, or y in place of the error .TP \fBa ?? b ?? c\fR the first of them that holds a value; only the last one is never null .TP \fBx ?? return value\fR the value, or a return from the function .TP \fBx ?? panic("..."), x ?? exit(code)\fR the value, or the program stops .TP \fB(x ?? 0) + 1, (x ?? 0) == 1\fR ?? binds more loosely than +, == and &&, so it is put in parentheses .SH DESCRIPTION \fBx ?? y\fR opens a value that may be missing. When \fBx\fR holds a value, that value is the result and \fBy\fR is never computed. When \fBx\fR is \fBnull\fR, the result is \fBy\fR\&. On a \fBResult\fR it is the same: the value of \fBOk\fR, or \fBy\fR in place of the error, which is let go. .PP The result is a \fBT\fR, never a \fBT?\fR, so \fBy\fR must be a \fBT\fR: a \fBT?\fR on the right needs a \fB??\fR of its own, and \fBa ?? b ?? c\fR groups as \fBa ?? (b ?? c)\fR\&. A borrow of a value that can be copied gives the value: \fBxs.get(i) ?? 0\fR is an \fBint\fR\&. On a \fBread T?\fR the result is a \fBread T\fR, with nothing taken out. On a \fBT?\fR that owns what it holds, as a \fBstring?\fR does, \fB??\fR takes the value: the variable is moved, and \fBread x ?? y\fR only looks. .PP The right side may also leave instead of giving a value. \fB?? return ...\fR returns from the function, and \fBpanic(...)\fR and \fBexit(code)\fR stop the program; after such a line the name holds a \fBT\fR\&. \fBreturn\fR is the only statement written there: there is no \fB?? break\fR or \fB?? continue\fR\&. .PP \fB??\fR binds more loosely than every operator but \fB?:\fR\&. \fBn ?? 0 * 10\fR is \fBn ?? (0 * 10)\fR, and a sum or a comparison of the result is written \fB(n ?? 0) * 10\fR\&. There is no \fB??=\fR\&. .PP \fB??\fR is a test of a flag, or of a pointer for a \fBT*?\fR, as an \fBif\fR is. In compiled code, an \fBawait\fR on the right of \fB??\fR is not taken yet: see [\fBawait\fR](await.md). .SH EXAMPLES .SS "A value in place of null" .RS 4 .nf void main() { int? age = null print(age ?? 0) age = 36 print(age ?? 0) string? nickname = null print(nickname ?? "no nickname") var scores = List { 7, 9 } print(scores.get(1) ?? 0) print(scores.get(5) ?? 0) } .fi .RE .PP .RS 4 .nf 0 36 no nickname 9 0 .fi .RE .PP \fBscores.get(i)\fR gives \fBnull\fR past the end of the list, so the last line prints the fallback. .SS "In place of an error" .RS 4 .nf void main() { for text in "12 x7 30".splitWhitespace() { var n = int.parse(text) ?? -1 print($"{text} -> {n}") } } .fi .RE .PP .RS 4 .nf 12 -> 12 x7 -> -1 30 -> 30 .fi .RE .PP \fBint.parse\fR gives a \fBResult\fR\&. \fB??\fR drops the error and gives \fB\-1\fR in its place. .SS "The right side runs only when it is needed" .RS 4 .nf int slowDefault() { print("computing the default") return 0 } void main() { int? cached = 5 print(cached ?? slowDefault()) cached = null print(cached ?? slowDefault()) } .fi .RE .PP .RS 4 .nf 5 computing the default 0 .fi .RE .PP \fBslowDefault\fR is called once: the first time, \fBcached\fR held a value. .SS "Several fallbacks" .RS 4 .nf void show(read string? fromFlag, read string? fromConfig) { print(fromFlag ?? fromConfig ?? "default.toml") } void main() { show("app.toml", "config.toml") show(null, "config.toml") show(null, null) } .fi .RE .PP .RS 4 .nf app.toml config.toml default.toml .fi .RE .PP The chain takes the first value it finds. Only \fB"default.toml"\fR, at the end, is never \fBnull\fR\&. .SS "Parentheses" .RS 4 .nf void main() { int? n = 4 print(n ?? 0 * 10) print((n ?? 0) * 10) print((n ?? 0) == 4) } .fi .RE .PP .RS 4 .nf 4 40 true .fi .RE .PP The first line is \fBn ?? (0 * 10)\fR, which is \fB4\fR\&. .SS "Looking without taking" .RS 4 .nf void main() { string? nickname = "Pasha".clone() var shown = read nickname ?? "nobody" print(shown) print(nickname ?? "nobody") } .fi .RE .PP .RS 4 .nf Pasha Pasha .fi .RE .PP \fBnickname\fR owns its text, and \fBnickname ?? ...\fR would take it. \fBread nickname ?? ...\fR gives a \fBread string\fR that looks at it, so \fBnickname\fR can be used again on the next line. .SS "Leaving the function" .RS 4 .nf int doubleFirst(read List numbers) { var first = numbers.get(0) ?? return -1 return first * 2 } Result portOf(read string text) { var port = int.parse(text) ?? return Error($"not a port: {text}".toString()) return Ok(port) } void main() { print(doubleFirst(read List { 21 })) print(doubleFirst(read List())) match portOf("80x") { Ok(port) => print(port) Error(why) => print(why) } } .fi .RE .PP .RS 4 .nf 42 -1 not a port: 80x .fi .RE .PP After the line with \fB?? return\fR, \fBfirst\fR is an \fBint\fR and \fBport\fR an \fBint\fR: the case where they were missing has left. .SS "With await" .RS 4 .nf async int? lookup(read string key) { await Task.delay(5) return key == "answer" ? 42 : null } async void main() { print(await lookup("answer") ?? 0) print(await lookup("question") ?? 0) } .fi .RE .PP .RS 4 .nf 42 0 .fi .RE .PP \fBawait\fR binds more tightly than \fB??\fR: the task is waited for first, and \fB??\fR opens what it gave. .SH "WHAT STOPS THE PROGRAM" .SS "?? panic" .RS 4 .nf void main() { var settings = HashMap { {"host", "localhost"} } print(settings.get("host") ?? panic("no host")) print(settings.get("port") ?? panic("no port")) } .fi .RE .PP .RS 4 .nf localhost panic: no port --> example.sg:4:35 4 | print(settings.get("port") ?? panic("no port")) | ^^^^^^^^^^^^^^^^ .fi .RE .PP \fBsettings.get\fR gives a \fBread string?\fR, \fBnull\fR for a key that is not there. .SH "WHAT THE COMPILER REFUSES" .SS "A value that is never null" .RS 4 .nf void main() { int count = 5 print(count ?? 0) } .fi .RE .PP .RS 4 .nf error: this is `int`, which is never `null`, so `??` has nothing to do --> example.sg:3:11 3 | print(count ?? 0) | ^^^^^ .fi .RE .SS "A fallback that may be null too" .RS 4 .nf void main() { int? a = null int? b = null int? c = a ?? b print(c ?? 0) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `int?` --> example.sg:4:19 4 | int? c = a ?? b | ^ hint: it may be `null`: give a fallback with `b ?? ...`, or test it with `is null` .fi .RE .SS "A Result that carries nothing" .RS 4 .nf Result save() { return Ok() } void main() { var done = save() ?? 0 print(done) } .fi .RE .PP .RS 4 .nf error: this is `Result`: success carries no value, so `??` has nothing to give --> example.sg:6:16 6 | var done = save() ?? 0 | ^^^^^^ hint: pass the error on with `try`, or drop it on purpose with `_ = ...` .fi .RE .SS "A value used after ?? took it" .RS 4 .nf void main() { string? nickname = "Pasha".clone() print(nickname ?? "nobody") print(nickname ?? "nobody") } .fi .RE .PP .RS 4 .nf error: `nickname` was moved and can no longer be used --> example.sg:4:11 4 | print(nickname ?? "nobody") | ^^^^^^^^ it was moved here: --> example.sg:3:11 3 | print(nickname ?? "nobody") | ^^^^^^^^ hint: `??` takes what `nickname` holds; to look at it without taking it: `read nickname ?? ...` .fi .RE .PP \fBread nickname ?? "nobody"\fR looks, as in the example above. .SS "A comparison without parentheses" .RS 4 .nf void main() { int? n = 4 print(n ?? 0 == 4) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `bool` `??` binds more loosely than `==`: this reads `n ?? (0 == 4)` --> example.sg:3:16 3 | print(n ?? 0 == 4) | ^^^^^^ hint: to compare the value, write `(n ?? 0) == 4` .fi .RE .PP \fBn == 4\fR alone compares too: a \fBT?\fR is compared with a \fBT\fR as a value that is there, and \fBnull\fR is equal to nothing but \fBnull\fR\&. .SS "break on the right" .RS 4 .nf void main() { var total = 0 for text in "1 x 3".splitWhitespace() { var n = int.parse(text) ?? break total += n } print(total) } .fi .RE .PP .RS 4 .nf error: `break` cannot stand on the right of `??`: only `return` leaves from there --> example.sg:4:36 4 | var n = int.parse(text) ?? break | ^^^^^ hint: test it with `is`: on a `Result`, `if int.parse(text) is not Ok(n) { break }`; on a `T?`, `var n = int.parse(text)`, then `if n is null { break }` .fi .RE .PP \fBint.parse\fR gives a \fBResult\fR, so the first form of the hint is the one here; after that \fBif\fR, \fBn\fR is an \fBint\fR\&. .SS "??=" .RS 4 .nf void main() { int? limit = null limit ??= 10 print(limit ?? 0) } .fi .RE .PP .RS 4 .nf error: there is no `??=` --> example.sg:3:11 3 | limit ??= 10 | ^^^ hint: `limit = limit ?? 10` .fi .RE .SH "SEE ALSO" .BR sg-nullable (7), .BR sg-null (7), .BR sg-try (7), .BR sg-return (7), .BR sg-is (7), .BR sg-await (7) .PP https://sg\-lang.org/reference/null-coalescing.html