.\" Made by website/plain/build.mjs from website/words/null-conditional.md; change that file. .TH SG-NULL-CONDITIONAL 7 2026-11-28 "SG" "SG reference" .SH NAME x?.y \- a field or a method of x, or null when x is null .SH SYNOPSIS .TP \fBx?.field\fR the field of a T?, or null when x is null .TP \fBx?.method(a)\fR what the method gives, or null; a is computed only when x holds a value .TP \fBx?.a.b, x?.a?.b\fR a chain: once a part is null, the rest does not run .TP \fBx?.field ?? value\fR the field, or a value in its place .TP \fBx?.change()\fR on a line of its own: a method that gives nothing, called only when x holds a value .TP \fBx?.field is not null\fR a test that narrows x too .SH DESCRIPTION \fBx?.y\fR reaches into a value that may be missing. When \fBx\fR holds a value, it is \fBx.y\fR; when \fBx\fR is \fBnull\fR, it is \fBnull\fR, and nothing to the right of \fB?.\fR runs: no field is read, no method is called, and the arguments of the method are not computed. .PP The result is a \fBU?\fR, where \fBU\fR is the type of the end of the chain, and never a \fBU??\fR: a field that is a \fBT?\fR already gives a \fBT?\fR\&. One \fB?.\fR covers the whole chain after it, so \fBx?.a.b\fR needs a second \fB?.\fR only where \fBa\fR itself may be \fBnull\fR\&. The result is mostly given to \fB??\fR, tested with \fBis\fR, or kept in a variable. .PP A field that can be copied is copied: \fBp?.age\fR is an \fBint?\fR\&. A field that cannot is lent, not moved: \fBp?.name\fR is a \fBread string?\fR, and \fBp?.name.clone()\fR owns its text. A method is called on the place itself, lent to look or to change as the method asks. .PP \fB?.\fR is written on a \fBT?\fR, a \fBread T?\fR and a \fBT*?\fR\&. On a value that is never \fBnull\fR it is an error, with the hint to write \fB\&.\fR; on a \fBResult\fR it is an error too, since a \fBResult\fR holds an error, not \fBnull\fR: \fBtry\fR or \fB??\fR open it. \fB?.\fR only reads: nothing is assigned through it, and there is no \fB?[i]\fR\&. .PP \fB?.\fR costs one test of the flag, or of the pointer for a \fBT*?\fR, as an \fBif\fR would. .SH EXAMPLES .SS "A field that may be missing" .RS 4 .nf struct Address { string city } struct Person { string name Address? address } void main() { var people = List { Person { name = "Ada", address = Address { city = "London" } }, Person { name = "Pasha", address = null } } for person in read people { var city = person.address?.city ?? "unknown" print($"{person.name}: {city}") } } .fi .RE .PP .RS 4 .nf Ada: London Pasha: unknown .fi .RE .PP \fBperson.address\fR is an \fBAddress?\fR, so \fBperson.address?.city\fR is a \fBread string?\fR, and \fB??\fR gives a text in place of \fBnull\fR\&. .SS "Methods" .RS 4 .nf void show(read string? name) { var letters = name?.length() ?? 0 print($"{letters} letters, {name?.toUpper() ?? "-"}") } void main() { show("Ada") show(null) } .fi .RE .PP .RS 4 .nf 3 letters, ADA 0 letters, - .fi .RE .PP \fBname?.length()\fR is a \fBnint?\fR, \fBnull\fR when there is no name. .SS "A chain" .RS 4 .nf struct Address { string city int zip } struct Person { string name Address? address } void main() { Person? ada = Person { name = "Ada", address = Address { city = "London", zip = 1815 } } Person? nobody = null print(ada?.address?.city ?? "?") print(nobody?.address?.city ?? "?") var zip = ada?.address?.zip print(zip ?? 0) print(ada?.name.length() ?? 0) } .fi .RE .PP .RS 4 .nf London ? 1815 3 .fi .RE .PP \fBzip\fR is an \fBint?\fR, not an \fBint??\fR\&. In \fBada?.name.length()\fR, \fBname\fR is never \fBnull\fR, so one \fB?.\fR is enough. .SS "Arguments that do not run" .RS 4 .nf int next() { print("next runs") return 1 } struct Box { int n int plus(read this, int k) { return this.n + k } } void main() { Box? none = null print(none?.plus(next()) ?? 0) Box? some = Box { n = 1 } print(some?.plus(next()) ?? 0) } .fi .RE .PP .RS 4 .nf 0 next runs 2 .fi .RE .PP When \fBnone\fR is \fBnull\fR, \fBplus\fR is not called, and its argument \fBnext()\fR is not computed either. .SS "A method that changes" .RS 4 .nf struct Counter { int n void bump(ref this) { this.n += 1 } } void main() { Counter? clicks = Counter { n = 0 } clicks?.bump() clicks?.bump() print(clicks?.n ?? -1) clicks = null clicks?.bump() print(clicks?.n ?? -1) } .fi .RE .PP .RS 4 .nf 2 -1 .fi .RE .PP \fBbump\fR gives nothing, so \fBclicks?.bump()\fR stands on a line of its own. It changes \fBclicks\fR where it is, and does nothing when \fBclicks\fR is \fBnull\fR\&. .SS "A test that narrows" .RS 4 .nf struct Address { string city } struct Person { Address? address } void main() { Person? p = Person { address = Address { city = "Rome" } } if p?.address is not null { print(p.address?.city ?? "") } } .fi .RE .PP .RS 4 .nf Rome .fi .RE .PP Where \fBp?.address is not null\fR holds, \fBp\fR is not \fBnull\fR either, so inside the \fBif\fR it is a \fBPerson\fR and is reached with \fB\&.\fR\&. .SS "Through a pointer" .RS 4 .nf struct Node { int value Node*? next } void main() { Node*? head = new Node { value = 1, next = new Node { value = 2, next = null } } print(head?.value ?? 0) print(head?.next?.value ?? 0) print(head?.next?.next?.value ?? 0) } .fi .RE .PP .RS 4 .nf 1 2 0 .fi .RE .PP On a \fBNode*?\fR the test is of the pointer: \fBnull\fR is the address zero. .SS "Owning what a chain lends" .RS 4 .nf struct Person { string name } string nameOf(read Person? person) { return person?.name.clone() ?? "nobody".clone() } void main() { Person? ada = Person { name = "Ada" } var name = nameOf(read ada) print(name) print(nameOf(null)) } .fi .RE .PP .RS 4 .nf Ada nobody .fi .RE .PP \fBperson?.name\fR is a \fBread string?\fR that looks into \fBada\fR\&. \fB\&.clone()\fR at the end of the chain makes a \fBstring?\fR that owns its text, so it can be returned. .SH "WHAT THE COMPILER REFUSES" .SS "A value that is never null" .RS 4 .nf struct Point { int x } void main() { var p = Point { x = 1 } print(p?.x) } .fi .RE .PP .RS 4 .nf error: `p` is a `Point`, which is never `null` --> example.sg:7:11 7 | print(p?.x) | ^ hint: write `.` instead of `?.` .fi .RE .SS "A Result" .RS 4 .nf void main() { var r = int.parse("-4") print(r?.abs() ?? 0) } .fi .RE .PP .RS 4 .nf error: `?.` goes past a `null`, and `r` is a `Result`, which holds a value or an error --> example.sg:3:11 3 | print(r?.abs() ?? 0) | ^ hint: `(try r)` passes the error on, and `(r ?? fallback)` puts a value in its place .fi .RE .SS "A method that gives nothing, used as a value" .RS 4 .nf struct Counter { int n void bump(ref this) { this.n += 1 } } void main() { Counter? c = Counter { n = 1 } var done = c?.bump() } .fi .RE .PP .RS 4 .nf error: `c?.bump()` gives nothing, so `?.` has no value to give --> example.sg:11:16 11 | var done = c?.bump() | ^^^^^^^^^ hint: write it on a line of its own .fi .RE .SS "Owning a lent field" .RS 4 .nf struct Person { string name } void main() { Person? p = Person { name = "Ada" } string? name = p?.name print(name ?? "") } .fi .RE .PP .RS 4 .nf error: expected `string?`, found `read string?` --> example.sg:7:20 7 | string? name = p?.name | ^^^^^^^ hint: `read string?` only borrows the text; to own a copy write `p?.name.clone()` .fi .RE .PP The types named are those of the whole \fBp?.name\fR\&. A method that gives another type is named the same way: \fBint? n = name?.length()\fR is refused as \fBexpected `int?`, found `nint?` \fR\&. .SS "Assigning through ?." .RS 4 .nf struct Point { int x } void main() { Point? p = Point { x = 1 } p?.x = 5 } .fi .RE .PP .RS 4 .nf error: `?.` only reads: a field is written through a value that is there --> example.sg:7:5 7 | p?.x = 5 | ^^^^ hint: test it first: `if p is not null { p.x = 5 }` .fi .RE .SS "An item through ?." .RS 4 .nf void main() { List? ys = List { 1 } print(ys?[0]) } .fi .RE .PP .RS 4 .nf error: there is no `?[i]`: `?.` reaches fields and methods, not items --> example.sg:3:11 3 | print(ys?[0]) | ^^^^^^ hint: `ys?.get(0)` gives `null` when `ys` is `null` or has no such item; or test it first: `if ys is not null { ... ys[0] ... }` .fi .RE .SH "SEE ALSO" .BR sg-nullable (7), .BR sg-null (7), .BR sg-null-coalescing (7), .BR sg-is (7), .BR sg-try (7) .PP https://sg\-lang.org/reference/null-conditional.html