.\" Made by website/plain/build.mjs from website/words/nullable.md; change that file. .TH SG-NULLABLE 7 2026-11-28 "SG" "SG reference" .SH NAME T? \- a T that may be null; it is tested before it is used as a T .SH SYNOPSIS .TP \fBint?, string?, Point?, Node*?\fR a value of the type, or null; every type has one .TP \fBread string?\fR a borrow that may be missing: a pointer, null when there is nothing .TP \fBx is null, x is not null\fR the test; where x is sure not to be null, it is a T .TP \fBx ?? fallback\fR the value, or the fallback when it is null .TP \fBx ?? return null, x ?? panic("...")\fR the value, or leave the function, or stop the program .TP \fBx?.field, x?.method()\fR the field or the result, or null when x is null .TP \fBmatch x { null => ... var v => ... }\fR an arm for null; below it, v is a T .TP \fBtry x\fR the value, or null handed back by a function that returns a T? .TP \fBx.take()\fR the value, with null left in its place .SH DESCRIPTION No type can be empty by accident: an \fBint\fR is always a number and a \fBstring\fR always a text. A value that may be missing says so with \fB?\fR: an \fBint?\fR is an \fBint\fR or \fBnull\fR\&. It is the type of a field that need not be there, of a function that may find nothing, and of the library's methods that look something up: \fBlist.get(i)\fR, \fBmap.get(key)\fR, \fBlist.removeLast()\fR, \fBn.tryToInt()\fR\&. .PP A \fBT?\fR is not a \fBT\fR\&. Its value is used only after a test, and the compiler follows the test: after \fBif x is not null\fR, in the block, \fBx\fR is a \fBT\fR\&. The same holds on the right of \fB&&\fR, in \fBelse\fR after \fBif x is null\fR, after an \fBif x is null\fR whose block always leaves, and after \fBwhile x is null\fR\&. Only local variables and parameters are narrowed this way, never a field, which a call in between could change; a field is lent to a variable, and the variable tested. An assignment of a \fBT?\fR or of \fBnull\fR, or \fBtake()\fR, ends what the test said; an assignment of a \fBT\fR keeps it. .PP \fB??\fR gives a fallback in one step, and on its right a \fBreturn\fR or a \fBpanic\fR leaves instead. \fB?.\fR reads through a \fBT?\fR: \fBuser?.name\fR is \fBnull\fR when \fBuser\fR is, and one \fB?.\fR covers the rest of the chain, so there is never a \fBT??\fR\&. \fB==\fR compares a \fBT?\fR with a \fBT?\fR or a \fBT\fR: equal when both are \fBnull\fR or both hold equal values. A \fBT?\fR is not a condition (\fBif name is not null\fR is the test), and it cannot be printed or written into text alone; inside a list or a struct that is printed, it is written \fBnull\fR\&. .PP A \fBT?\fR costs a flag. An \fBint?\fR is the number and a \fBbool\fR beside it, 8 bytes where an \fBint\fR takes 4. A \fBT*?\fR takes nothing more: it is the pointer of a \fBT*\fR, with \fBnull\fR as the address zero. A \fBread T?\fR is a pointer too (for text, the pointer and the length of a \fBread string\fR), and \fBlist.get(i)\fR gives one into the list, with nothing copied. .SH EXAMPLES .SS "Test, then use" .RS 4 .nf void main() { int? age = null print(age is null) print(age ?? 0) age = 36 if age is not null { print(age + 1) } } .fi .RE .PP .RS 4 .nf true 0 37 .fi .RE .PP \fBage\fR is an \fBint?\fR everywhere, and an \fBint\fR only inside the block of the test, where \fBage + 1\fR is allowed. .SS "A function that may find nothing" .RS 4 .nf int? find(read List names, read string wanted) { nint i = 0 for name in names { if name == wanted { return (int)i } i += 1 } return null } void main() { var names = List { "Ada", "Grace", "Pasha" } print(find(read names, "Bob") ?? -1) var at = find(read names, "Grace") if at is null { print("not there") return } print($"found at {at}, the next is {at + 1}") } .fi .RE .PP .RS 4 .nf -1 found at 1, the next is 2 .fi .RE .PP \fBreturn (int)i\fR gives an \fBint\fR, which becomes an \fBint?\fR on its own. After the \fBif\fR, whose block returns, \fBat\fR is an \fBint\fR\&. .SS "From the library" .RS 4 .nf void main() { var names = List { "Ada", "Grace" } var ages = HashMap { {"Ada", 36} } var first = names.get(0) if first is not null { print($"{first} has {first.length()} letters") } print(names.get(9) ?? "nobody") print(ages.get("Ada") ?? 0) print(ages.get("Bob") ?? 0) long big = 5_000_000_000 print(big.tryToInt() ?? -1) print(names.removeLast() ?? "empty") } .fi .RE .PP .RS 4 .nf Ada has 3 letters nobody 36 0 -1 Grace .fi .RE .PP \fBnames.get(0)\fR gives a \fBread string?\fR, a look into the list with nothing copied. Where \fBnames[9]\fR would stop the program, \fBget(9)\fR gives \fBnull\fR\&. .SS "match, with an arm for null" .RS 4 .nf string describe(int? code) { return match code { null => "no answer" 200 => "fine" 400..=499 => "your mistake" var other => $"class {other / 100}xx".toString() } } void main() { print(describe(null)) print(describe(200)) print(describe(404)) print(describe(503)) } .fi .RE .PP .RS 4 .nf no answer fine your mistake class 5xx .fi .RE .PP Below the arm \fBnull\fR, \fBother\fR is an \fBint\fR\&. Every other arm tests the number, as on an \fBint\fR\&. .SS "Comparing two that may be missing" .RS 4 .nf void main() { int? saved = 3 int? typed = 3 int? none = null print(saved == typed) print(saved == none) print(saved == 3) } .fi .RE .PP .RS 4 .nf true false true .fi .RE .PP Two \fBT?\fR are equal when both hold equal values or both are \fBnull\fR, as a \fBT?\fR field compares in a struct. A \fBT\fR on one side is compared as a value that is there. \fBx == null\fR is not written: the test is \fBx is null\fR\&. .SS "A field that may be missing" .RS 4 .nf struct Address { string city string? street } struct Person { string name Address? address } void show(read Person person) { var city = person.address?.city ?? "unknown" var street = person.address?.street ?? "no street" print($"{person.name}: {city}, {street}") var address = read person.address if address is not null && address.street is null { print($" {address.city} has no street") } } void main() { var ada = Person { name = "Ada", address = Address { city = "London", street = "St James's Square" } } var grace = Person { name = "Grace", address = Address { city = "New York", street = null } } var pasha = Person { name = "Pasha", address = null } show(read ada) show(read grace) show(read pasha) } .fi .RE .PP .RS 4 .nf Ada: London, St James's Square Grace: New York, no street New York has no street Pasha: unknown, no street .fi .RE .PP \fBperson.address?.street\fR is a \fBread string?\fR: \fBnull\fR when there is no address and when there is no street. The field \fBperson.address\fR is not narrowed, so it is lent to \fBaddress\fR, which is. .SS "Handing null on" .RS 4 .nf int? sumOfFirstTwo(read List fields) { var a = try fields.get(0) var b = try fields.get(1) var x = int.parse(a) ?? return null var y = int.parse(b) ?? return null return x + y } void main() { print(sumOfFirstTwo(read List { "2", "40" }) ?? -1) print(sumOfFirstTwo(read List { "2" }) ?? -1) print(sumOfFirstTwo(read List { "2", "x" }) ?? -1) } .fi .RE .PP .RS 4 .nf 42 -1 -1 .fi .RE .PP In a function that returns a \fBT?\fR, \fBtry\fR on a \fBT?\fR returns \fBnull\fR at once. \fBint.parse\fR gives a \fBResult\fR, so it is opened with \fB??\fR, whose right side returns. .SS "A pointer that may be null" .RS 4 .nf struct Node { int value Node*? next } void main() { Node*? list = null for v in 1..4 { list = new Node { value = v, next = list.take() } } int sum = 0 var cur = read list while cur is not null { sum += cur.value cur = read cur.next } print(sum) print(list?.value ?? 0) } .fi .RE .PP .RS 4 .nf 6 3 .fi .RE .PP A \fBNode*\fR always points to a node; the end of the list is a \fBNode*?\fR that is \fBnull\fR\&. \fBtake()\fR moves the old list into the new node and leaves \fBnull\fR in \fBlist\fR until the assignment fills it again. .SH "WHAT STOPS THE PROGRAM" .SS "A value that had to be there" .RS 4 .nf void main() { var ports = HashMap { {"http", 80}, {"https", 443} } print(ports.get("https") ?? panic("no port for https")) print(ports.get("gopher") ?? panic("no port for gopher")) } .fi .RE .PP .RS 4 .nf 443 panic: no port for gopher --> example.sg:4:34 4 | print(ports.get("gopher") ?? panic("no port for gopher")) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ .fi .RE .PP \fB?? panic(...)\fR says that a missing value is a bug. Where the data can really be missing, a fallback or a \fBResult\fR is the way. .SH "WHAT THE COMPILER REFUSES" .SS "Using it before a test" .RS 4 .nf int? find(read List xs, int wanted) { nint i = 0 for x in xs { if x == wanted { return (int)i } i += 1 } return null } void main() { var xs = List { 4, 8, 15 } var at = find(read xs, 8) print(at + 1) } .fi .RE .PP .RS 4 .nf error: operator `+` is not defined for `int?` and `int` `at` may be `null` --> example.sg:15:11 15 | print(at + 1) | ^^^^^^ hint: test it first: `if at is not null { ... }`; or give a fallback with `??` .fi .RE .SS "Printing it as it is" .RS 4 .nf void main() { var ages = HashMap { {"Ada", 36} } var age = ages.get("Ada") print(age) } .fi .RE .PP .RS 4 .nf error: this is `int?` and may be `null`; a `T?` alone is not written as text inside a list or a struct a missing value is written `null` --> example.sg:4:11 4 | print(age) | ^^^ hint: give a fallback, `age ?? ...`, or test it first: `if age is not null { ... }` .fi .RE .SS "A field after its test" .RS 4 .nf struct User { string name string? nickname } void main() { var user = User { name = "Ada", nickname = "Countess" } if user.nickname is not null { print(user.nickname.length()) } } .fi .RE .PP .RS 4 .nf error: this is `string?` and may be `null`; `.length()` needs a value --> example.sg:9:15 9 | print(user.nickname.length()) | ^^^^^^^^^^^^^ hint: a field is not narrowed: lend it to a variable and test that: `var nickname = read user.nickname`, then `if nickname is not null { ... }` .fi .RE .SS "A variable given null after its test" .RS 4 .nf void main() { int? count = 5 if count is not null { count = null print(count + 1) } } .fi .RE .PP .RS 4 .nf error: operator `+` is not defined for `int?` and `int` `count` may be `null` --> example.sg:5:15 5 | print(count + 1) | ^^^^^^^^^ .fi .RE .PP An assignment of \fBnull\fR, or of any \fBT?\fR, ends what the test said: \fBcount\fR is an \fBint?\fR again. One of a \fBT\fR keeps it: after \fBcount = 9\fR, \fBcount + 1\fR is taken. So does \fBcount += 1\fR, which is \fBcount = count + 1\fR; on a \fBcount\fR that no test narrowed it is refused, with the test as its hint. .SS "A T? as a condition" .RS 4 .nf void main() { string? name = null if name { print("named") } } .fi .RE .PP .RS 4 .nf error: expected `bool`, found `string?` --> example.sg:3:8 3 | if name { | ^^^^ 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 .SS "A match with no arm for null" .RS 4 .nf enum Shape { Circle(int radius) Square(int side) } int area(read Shape? shape) { return match shape { Circle(r) => 3 * r * r Square(s) => s * s } } void main() { print(area(read Shape.Square(2))) } .fi .RE .PP .RS 4 .nf error: this `match` does not handle `null` --> example.sg:7:18 7 | return match shape { | ^^^^^ hint: add an arm for it, or `_ => ...` for everything else .fi .RE .SS "A T??" .RS 4 .nf alias MaybeInt = int? void main() { MaybeInt? count = null print(count ?? 0) } .fi .RE .PP .RS 4 .nf error: `MaybeInt` is `int?` already, and a value is either there or `null` --> example.sg:4:5 4 | MaybeInt? count = null | ^^^^^^^^^ hint: `MaybeInt` .fi .RE .PP A value is there or it is not; a second \fB?\fR would say nothing more. Written out, \fBint?? count = null\fR gets the same message, with a hint that writes one \fB?\fR: \fBint?\fR\&. .SH "SEE ALSO" .BR sg-null (7), .BR sg-is (7), .BR sg-null-coalescing (7), .BR sg-null-conditional (7), .BR sg-match (7), .BR sg-try (7), .BR sg-pointer (7) .PP https://sg\-lang.org/reference/nullable.html