.\" Made by website/plain/build.mjs from website/words/weak.md; change that file. .TH SG-WEAK 7 2026-11-28 "SG" "SG reference" .SH NAME weak \- weak T: a look at a shared value that does not keep it alive; share asks for an owner .SH SYNOPSIS .TP \fBweak x\fR a look at the value the owner x points to; of a shared T?, a weak T? .TP \fBweak T, weak T?\fR the type of a look, one that may be missing .TP \fBweak checked T, weak locked T\fR a look at a shared checked or a shared locked value .TP \fBshare w\fR an owner from the look: a shared T?, null once the value is gone .TP \fBw is null\fR on a weak T?: whether it looks at anything at all .TP \fBweak this\fR in a method of read Ref this: a look at the value it was called on .SH DESCRIPTION \fBweak x\fR takes an owner, a \fBshared T\fR, and gives a \fBweak T\fR: it looks at the same value but does not own it. The value is let go when its last owner ends, however many looks remain. \fBweak w\fR of a look is one more look at the same value. .PP A look is never followed by itself, since what it looks at may be gone. A \fBweak\fR value is not printed, compared or reached into, and it is not opened with \fBread ... as\fR or \fBlock\fR\&. \fBshare w\fR asks for an owner first: a \fBshared T?\fR, which is the value while it is alive and \fBnull\fR once it is gone. Once tested, that owner keeps the value alive for as long as it lives. .PP A \fBweak T?\fR may also be missing. There is one \fBnull\fR for both cases: \fBw is null\fR asks whether it looks at anything, and \fBshare w is null\fR whether what it looks at is still alive. A program asks both only where the difference matters. .PP What \fBweak\fR is for is a circle. Owners that point at each other, as a parent and its children, keep each other alive, and nothing frees them. The side that does not own is made \fBweak\fR: a child's link to its parent, a link back in a list, a friend. The interpreter, the tests and a debug build report a circle that is left when the program ends, with the field that holds it; a release build does not. .PP A look keeps the same word as the owner: \fBweak checked T\fR looks at a \fBshared checked T\fR, \fBweak locked T\fR at a \fBshared locked T\fR, and \fBshare\fR gives that owner back. A \fBweak T\fR and a \fBweak locked T\fR go to another task, a \fBweak checked T\fR does not, as its owner does not. \fBshare w\fR is one atomic step, which adds an owner only while there is one. .SH EXAMPLES .SS "A look that does not keep it alive" .RS 4 .nf struct Config { string host } void main() { var config = new shared Config { host = "localhost" } var look = weak config var owner = share look config = new shared Config { host = "example.org" } print(owner?.host ?? "gone") owner = null owner = share look print(owner?.host ?? "gone") } .fi .RE .PP .RS 4 .nf localhost gone .fi .RE .PP When \fBconfig\fR gets a new value, \fBowner\fR is still an owner of the first one, so it stays. Once \fBowner\fR is \fBnull\fR, the first configuration has no owner left, and \fBshare look\fR gives \fBnull\fR\&. .SS "A link to the parent" .RS 4 .nf struct Folder { string name weak checked Folder? parent List children } shared checked Folder folder(string name, read shared checked Folder? parent) { var f = new shared checked Folder { name = name, parent = weak parent, children = List() } if parent is not null { parent.children.add(share f) } return f } void main() { var home = folder("home".clone(), null) var docs = folder("docs".clone(), read home) var notes = folder("notes".clone(), read docs) shared checked Folder? at = share notes while at is not null { print(at.name) at = share at.parent } print(home.children.length()) } .fi .RE .PP .RS 4 .nf notes docs home 1 .fi .RE .PP A folder owns its children and looks at its parent. \fBweak parent\fR of a \fBnull\fR parent is a \fBnull\fR look, so \fBshare at.parent\fR gives \fBnull\fR at the top and the walk ends. Were the parent owned too, each folder and its parent would keep each other alive. .SS "Linked, or alive" .RS 4 .nf struct Player { string name } struct Seat { int number weak Player? player } string who(read Seat seat) { if seat.player is null { return "empty" } var p = share seat.player if p is null { return "left" } return p.name.clone() } void main() { var ada = new shared Player { name = "Ada" } var seats = List { Seat { number = 1, player = weak ada }, Seat { number = 2, player = null } } for seat in read seats { print($"{seat.number}: {who(seat)}") } ada = new shared Player { name = "Grace" } for seat in read seats { print($"{seat.number}: {who(seat)}") } } .fi .RE .PP .RS 4 .nf 1: Ada 2: empty 1: left 2: empty .fi .RE .PP \fBseat.player is null\fR asks whether the seat was given a player; \fBshare seat.player\fR whether that player is still there. Seat 1 still looks at Ada after she is gone, and the look says so. .SS "Friends that look at each other" .RS 4 .nf struct Person { string name List friends } shared checked Person person(string name) { return new shared checked Person { name = name, friends = List() } } void main() { var ada = person("ada".clone()) var grace = person("grace".clone()) ada.friends.add(weak grace) grace.friends.add(weak ada) for look in read grace.friends { var friend = share look if friend is not null { print($"grace knows {friend.name}") } } ada = person("linus".clone()) var friend = share grace.friends[0] print(friend is null) } .fi .RE .PP .RS 4 .nf grace knows ada true .fi .RE .PP Each person looks at the other, so neither keeps the other alive. When \fBada\fR is given another person, the first one is let go, and grace's look at her gives \fBnull\fR\&. With \fBList\fR the two would hold each other for ever: see what stops the program below. .SS "A cache that does not keep what it holds" .RS 4 .nf struct Image { string name nint size } shared Image load(ref HashMap cache, string name) { var kept = cache.get(read name) if kept is not null { var image = share kept if image is not null { print($"{name}: from the cache") return image } } print($"{name}: loaded") var image = new shared Image { name = name.clone(), size = name.length() } _ = cache.remove(read name) cache.add(name, weak image) return image } void main() { var cache = HashMap() var a = load(ref cache, "cat.png") var b = load(ref cache, "cat.png") print(same(a, b)) a = load(ref cache, "dog.png") b = load(ref cache, "dog.png") _ = load(ref cache, "cat.png") } .fi .RE .PP .RS 4 .nf cat.png: loaded cat.png: from the cache true dog.png: loaded dog.png: from the cache cat.png: loaded .fi .RE .PP The cache gives an image again for as long as someone owns it. When \fBb\fR is given the dog, the cat has no owner left; its entry stays in the map, but its look gives \fBnull\fR, and the cat is loaded again. .SS "Looks in other tasks" .RS 4 .nf struct Log { List lines } bool write(weak locked Log look, string line) { var log = share look if log is null { return false } lock log as l { l.lines.add(line) } return true } void main() { var log = new shared locked Log { lines = List() } var a = Task.run(write, weak log, "one".clone()) var b = Task.run(write, weak log, "two".clone()) print($"{a.wait()} {b.wait()}") var look = weak log log = new shared locked Log { lines = List() } var c = Task.run(write, look, "three".clone()) print(c.wait()) lock log as l { print(l.lines.length()) } } .fi .RE .PP .RS 4 .nf true true false 0 .fi .RE .PP A task given a look does not keep the log alive. \fBshare look\fR in the task gives an owner while \fBmain\fR still has one; the third task starts after the first log is gone, so it writes nothing. .SH "WHAT STOPS THE PROGRAM" .SS "Owners in a circle" .RS 4 .nf struct Person { string name List friends } void main() { var ada = new shared checked Person { name = "ada".clone(), friends = List() } var grace = new shared checked Person { name = "grace".clone(), friends = List() } ada.friends.add(share grace) grace.friends.add(share ada) print(grace.friends[0].name) } .fi .RE .PP .RS 4 .nf ada leak: the program ended with 2 `shared` values still alive, which hold each other through `Person.friends` 1 x `Person`, made here: --> example.sg:7:15 7 | var ada = new shared checked Person { name = "ada".clone(), friends = List() } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 1 x `Person`, made here: --> example.sg:8:17 8 | var grace = new shared checked Person { name = "grace".clone(), friends = List() } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ hint: the side that does not own is `weak`: `List` .fi .RE .PP Each owns the other, so when \fBmain\fR ends both are still alive. The compiler cannot see a circle in the types, since a tree with none has the same types. The interpreter and a debug build end with this report and exit code 1, and a test that leaves a circle fails; a release build counts nothing and says nothing. .SH "WHAT THE COMPILER REFUSES" .SS "Reaching through a look" .RS 4 .nf struct Config { string host } void main() { var config = new shared Config { host = "localhost" } var look = weak config print(look.host) } .fi .RE .PP .RS 4 .nf error: this is `weak Config`: what it looks at may be gone, so `.host` cannot reach it --> example.sg:8:11 8 | print(look.host) | ^^^^ hint: ask for an owner first: `var owner = share look`, which is `null` if it is gone .fi .RE .SS "A look at a value with one owner" .RS 4 .nf struct City { int id } void main() { var home = City { id = 1 } var look = weak home } .fi .RE .PP .RS 4 .nf error: `weak` needs a `shared` value, and this is `City` --> example.sg:7:21 7 | var look = weak home | ^^^^ hint: a value with several owners is made with `new shared`: `var owner = new shared Node { ... }` .fi .RE .PP A value with one owner is borrowed with \fBread\fR or \fBref\fR, and the compiler sees that the borrow ends in time. .SS "new weak" .RS 4 .nf struct City { int id } void main() { var home = new weak City { id = 1 } } .fi .RE .PP .RS 4 .nf error: a `weak` value is not made with `new`: it looks at a `shared` one --> example.sg:6:20 6 | var home = new weak City { id = 1 } | ^^^^ hint: make it `shared` and look at it with `weak`: `var owner = new shared Node { ... }`, then `weak owner` .fi .RE .PP A look made with nothing else to own the value would be gone at once. .SS "Testing a look for null" .RS 4 .nf struct City { int id } void main() { var home = new shared City { id = 1 } var look = weak home if look is null { print("gone") } } .fi .RE .PP .RS 4 .nf error: this is `weak City`, which is never `null` only a `T?` can be `null` --> example.sg:8:8 8 | if look is null { | ^^^^ hint: to know whether what it looks at is gone: `share look is null` .fi .RE .PP A \fBweak City\fR always looks at something, which may be gone. Whether it is gone is asked of the owner \fBshare\fR gives, which is \fBnull\fR then. .SS "A weak checked look sent to a task" .RS 4 .nf struct Counter { int n } int peek(weak checked Counter look) { var c = share look return c?.n ?? 0 } void main() { var counter = new shared checked Counter { n = 1 } var task = Task.run(peek, weak counter) print(task.wait()) } .fi .RE .PP .RS 4 .nf error: `peek` takes a `weak checked Counter` here, which cannot go to another task: a `weak checked Counter` is checked within one task; a look at one that several tasks change is `weak locked` what goes to another task, comes back from one or is held by a `locked` value owns all it holds: no borrow, no `shared checked` value, no `Task` --> example.sg:12:31 12 | var task = Task.run(peek, weak counter) | ^^^^^^^^^^^^ .fi .RE .PP \fBshare\fR in the task would give a \fBshared checked\fR owner there, whose checks belong to one task. A value that tasks change is \fBshared locked\fR, and a task looks at it with \fBweak locked\fR\&. .SH "SEE ALSO" .BR sg-shared (7), .BR sg-share (7), .BR sg-checked (7), .BR sg-locked (7), .BR sg-new (7) .PP https://sg\-lang.org/reference/weak.html