.\" Made by website/plain/build.mjs from website/words/null.md; change that file. .TH SG-NULL 7 2026-11-28 "SG" "SG reference" .SH NAME null \- no value; only a type written T? may hold it, and it is tested with is null .SH SYNOPSIS .TP \fBint? count = null\fR a variable that holds nothing yet .TP \fBreturn null\fR a function that returns a T? gives nothing back .TP \fBgreet(null)\fR an argument for a parameter of a T? .TP \fBUser { nickname = null }, string? nickname = null\fR a field that holds nothing, and a default of a field .TP \fBx is null, x is not null\fR the test .TP \fBnull => ..., Go(null)\fR a pattern in a match, at the top or inside a variant .TP \fBready ? value : null\fR an arm of ?: that makes it a T? .TP \fBp == null\fR the test of a raw pointer, which is an address .SH DESCRIPTION \fBnull\fR is the value of a \fBT?\fR that holds nothing. There is no null of every type: an \fBint\fR, a \fBstring\fR, a struct and a \fBT*\fR always hold a value, and \fBnull\fR where one of them goes is an error. A pointer that may be missing is written \fBNode*?\fR; compiled, its \fBnull\fR is the address zero. .PP \fBnull\fR has no type of its own. It takes the type of the place it goes to, as a number does: a variable with its type written, a \fBreturn\fR, an argument, a field. So \fBvar x = null\fR is an error: nothing says what \fBx\fR would hold. .PP A \fBT?\fR is tested with \fBis null\fR and \fBis not null\fR, never with \fB== null\fR\&. The test narrows the variable: where \fBx is not null\fR holds, \fBx\fR is a \fBT\fR, and it stays one when a \fBT\fR is assigned to it; a \fBT?\fR or \fBnull\fR assigned ends that. In a \fBmatch\fR, an arm \fBnull\fR takes the missing value, and a name in an arm below it is a \fBT\fR; so is a name below \fBGo(null)\fR, in the same place of \fBGo\fR\&. Two \fBT?\fR values, or a \fBT?\fR and a \fBT\fR, are compared with \fB==\fR: they are equal when both are \fBnull\fR or both hold equal values. The one value compared with \fB== null\fR is a \fBraw\fR pointer, which is an address as in C and is not a \fBT?\fR\&. .PP \fBnull\fR is written as text only inside a value that is printed, such as a list or a struct: \fB[90, null, 75]\fR\&. A \fBT?\fR alone is not written: it is given a fallback with \fB??\fR, or tested, before it is printed. \fBshare\fR on a \fBweak\fR look gives \fBnull\fR once the value it looked at is gone. .SH EXAMPLES .SS "Where null goes" .RS 4 .nf struct User : Formattable { string name string? nickname = null int? age } int? half(int n) { if n % 2 != 0 { return null } return n / 2 } void greet(read string? name) { print($"hello, {name ?? "stranger"}") } void main() { int? count = null print(count ?? -1) print(half(7) ?? -1) greet(null) greet("Ada") var ada = User { name = "Ada", age = null } print(ada) } .fi .RE .PP .RS 4 .nf -1 -1 hello, stranger hello, Ada User { name = "Ada", nickname = null, age = null } .fi .RE .PP \fBnickname\fR has a default, so it is left out where \fBada\fR is built; \fBage\fR has none and is given \fBnull\fR by hand. .SS "null in a match" .RS 4 .nf enum Command { Go(string? target) Stop } void run(read Command command) { match command { Go(null) => print("go where?") Go("home") => print("going home") Go(target) => print($"going to {target}") Stop => print("stopping") } } string greeting(read string? name) { return match name { null => "hello, stranger" "Ada" => "welcome back, Ada" var other => $"hello, {other}, {other.length()} letters".toString() } } void main() { run(read Command.Go(null)) run(read Command.Go("home")) run(read Command.Go("Paris")) run(read Command.Stop) print(greeting(null)) print(greeting("Ada")) print(greeting("Grace")) } .fi .RE .PP .RS 4 .nf go where? going home going to Paris stopping hello, stranger welcome back, Ada hello, Grace, 5 letters .fi .RE .PP In \fBgreeting\fR, \fBother\fR is below the arm \fBnull\fR, so it is a \fBread string\fR with a \fBlength()\fR\&. In \fBrun\fR, \fBtarget\fR is below \fBGo(null)\fR, so it is a \fBread string\fR too, and is written with no fallback. .SS "Written as text" .RS 4 .nf struct Reading : Formattable { string place double? celsius } void main() { var scores = List { 90, null, 75 } print(scores) print(Reading { place = "Oslo", celsius = null }) print(Reading { place = "Rome", celsius = 21.5 }) } .fi .RE .PP .RS 4 .nf [90, null, 75] Reading { place = "Oslo", celsius = null } Reading { place = "Rome", celsius = 21.5 } .fi .RE .SS "A raw pointer" .RS 4 .nf void main() { raw int nothing = null var block = Memory.allocate(1) print(nothing == null) print(block == null) unsafe Memory.free(block) } .fi .RE .PP .RS 4 .nf true false .fi .RE .PP A \fBraw int\fR is an address, as C's \fBint*\fR is, and may be \fBnull\fR without a \fB?\fR\&. It is compared with \fB==\fR, and \fBis null\fR is refused on it. .SH "WHAT THE COMPILER REFUSES" .SS "null where a value must be" .RS 4 .nf int lengthOf(read string? text) { if text is null { return null } return (int)text.length() } void main() { print(lengthOf("Ada")) } .fi .RE .PP .RS 4 .nf error: `int` cannot be `null` --> example.sg:3:16 3 | return null | ^^^^ hint: a value that may be missing has the type `int?` .fi .RE .SS "A pointer that must point" .RS 4 .nf struct Node { int value Node* next } void main() { var last = new Node { value = 1, next = null } print(last.value) } .fi .RE .PP .RS 4 .nf error: `Node*` cannot be `null` --> example.sg:7:45 7 | var last = new Node { value = 1, next = null } | ^^^^ hint: a value that may be missing has the type `Node*?` .fi .RE .PP A \fBNode*\fR always owns a node, so a list whose end is \fBnull\fR writes its link \fBNode*? next\fR\&. .SS "var with null" .RS 4 .nf void main() { var best = null print(best ?? 0) } .fi .RE .PP .RS 4 .nf error: `var` cannot find a type from `null` alone --> example.sg:2:16 2 | var best = null | ^^^^ hint: write the type, for example `int? best = null` .fi .RE .SS "Comparing with ==" .RS 4 .nf void main() { var ages = HashMap { {"Ada", 36} } var age = ages.get("Bob") if age == null { print("no age") } } .fi .RE .PP .RS 4 .nf error: `null` is not compared with `==` --> example.sg:4:8 4 | if age == null { | ^^^^^^^^^^^ hint: write `age is null` .fi .RE .PP \fBage is null\fR is the test, and in its \fBelse\fR, \fBage\fR is narrowed. .SS "A test that is always false" .RS 4 .nf void main() { var names = List { "Ada" } var count = names.length() if count is null { print("empty") } } .fi .RE .PP .RS 4 .nf error: this is `nint`, which is never `null` only a `T?` can be `null` --> example.sg:4:8 4 | if count is null { | ^^^^^ hint: to test for zero: `count == 0` .fi .RE .PP An empty list has the length 0. .SS "An arm null on a value that has none" .RS 4 .nf void main() { int code = 200 match code { null => print("no code") _ => print(code) } } .fi .RE .PP .RS 4 .nf error: an `int` is never `null` only a value of a type written with `?` can be `null` --> example.sg:4:9 4 | null => print("no code") | ^^^^ .fi .RE .SH "SEE ALSO" .BR sg-nullable (7), .BR sg-is (7), .BR sg-match (7), .BR sg-null-coalescing (7), .BR sg-null-conditional (7), .BR sg-pointer (7), .BR sg-raw (7) .PP https://sg\-lang.org/reference/null.html