.\" Made by website/plain/build.mjs from website/words/checked.md; change that file. .TH SG-CHECKED 7 2026-11-28 "SG" "SG reference" .SH NAME checked \- shared checked T: a shared value changed through any owner, each borrow of it checked while the program runs .SH SYNOPSIS .TP \fBnew shared checked x\fR puts x on the heap and gives its first owner .TP \fBshared checked T, shared checked T?\fR the type of an owner, one that may be missing .TP \fBshare x\fR one more owner of the same value .TP \fBweak checked T\fR a look that does not keep the value alive .TP \fBread x as name { ... }\fR lends the value to read, for the block .TP \fBref x as name { ... }\fR lends the value to change, for the block .TP \fBalias FolderRef = shared checked Folder\fR a name for the type; new FolderRef { ... } makes one .SH DESCRIPTION A \fBshared T\fR is only read, since an owner that changed it could change it under a borrow another owner holds. A \fBshared checked T\fR is changed through any of its owners, as a \fBref\fR changes a value: its fields, a method that takes \fBref this\fR, a method that changes a collection, \fBfor x in ref\fR, and \fBref x\fR where a \fBref T\fR goes. \fBchecked\fR is written after \fBshared\fR or \fBweak\fR only; a value with one owner is changed through \fBref\fR\&. .PP Each borrow of it is checked while the program runs: the value is lent to one that changes it or to any number that read it, never both at once. A read or a write is checked once and borrows nothing after it, as in \fBa.count += 1\fR\&. A value lent to a call is borrowed until the call returns; the head of a \fBfor\fR, for the loop; the value a \fBmatch\fR or an \fBis\fR with names looks into, for the whole \fBmatch\fR or \fBif\fR; and \fBread x as n\fR or \fBref x as n\fR, for the block. A borrow also covers each \fBshared checked\fR value on the way to it: \fBread a.next.items\fR borrows \fBa\fR too, so nothing on the way is freed while it is lent. .PP A borrow of what it holds ends with its line, so a variable does not keep one, except inside an \fBas\fR block, where it lasts to the \fB}\fR\&. The receiver of a method that changes it is lent to read while the arguments are computed, and to change after them: \fBa.items.add(a.items.length())\fR works. .PP A conflict the compiler can see, through the same variable and the same fields, is a compile error: the line would stop the program every time it ran. Items of a list are left to run time, as for any list: \fBtransfer(ref accounts[i], ref accounts[j], 30)\fR on a \fBList\fR runs, and stops when \fBi\fR and \fBj\fR are the same. \fBref x\fR also works through a \fBread\fR borrow of an owner, or of a list of owners, since what is borrowed is the owner and not what it points to. One through another owner is found when it happens, and the program stops with the line of the access and the line where the borrow began. The checks are made in the interpreter and in every compiled build, release too, as an index is checked: they keep memory safe. They cost 16 bytes in front of the counts of owners, for the borrows in progress, and a write on each borrow. .PP The checks belong to one task. A \fBshared checked\fR value, or a \fBweak checked\fR look, does not go to another task, nor inside a field, a list or a \fBshared\fR value that goes. A value that several tasks change is \fBshared locked\fR; a value is \fBchecked\fR or \fBlocked\fR, not both. .SH EXAMPLES .SS "Changed through any owner" .RS 4 .nf struct Visits { int count List pages } struct Site { string name shared checked Visits visits } void main() { var visits = new shared checked Visits { count = 0, pages = List() } var shop = Site { name = "shop", visits = share visits } var blog = Site { name = "blog", visits = share visits } shop.visits.count += 1 shop.visits.pages.add("shop/cart") blog.visits.count += 1 blog.visits.pages.add("blog/news") print($"{visits.count} {visits.pages}") } .fi .RE .PP .RS 4 .nf 2 ["shop/cart", "blog/news"] .fi .RE .PP Both sites own the one \fBVisits\fR, and each changes it. Each line borrows the value only while it runs, so no two of them meet. .SS "Opened for a block" .RS 4 .nf struct Stock { List names List counts } void main() { var stock = new shared checked Stock { names = List { "tea", "milk", "bread" }, counts = List { 3, 0, 5 } } var shop = share stock ref shop as s { for c in ref s.counts { if c == 0 { c = 10 } } } read stock as s { var names = read s.names var counts = read s.counts for i in 0..names.length() { print($"{names[i]} {counts[i]}") } } } .fi .RE .PP .RS 4 .nf tea 3 milk 10 bread 5 .fi .RE .PP \fBref shop as s\fR lends the value to change until its \fB}\fR, and \fBread stock as s\fR to read. Inside the second block, \fBnames\fR and \fBcounts\fR keep a borrow over several lines, which outside a block they could not. .SS "Read twice at once, and changed in two steps" .RS 4 .nf struct Scores { List points } nint best(read Scores a, read Scores b) { nint top = 0 for p in read a.points { if p > top { top = p } } for p in read b.points { if p > top { top = p } } return top } void main() { var mine = new shared checked Scores { points = List { 3, 9 } } var team = share mine mine.points.add(mine.points.length()) team.points.add(mine.points.length() * 4) print(mine.points) print(best(read mine, read team)) } .fi .RE .PP .RS 4 .nf [3, 9, 2, 12] 12 .fi .RE .PP \fBmine\fR and \fBteam\fR are one value, lent twice to read in one call, which any number of readers may be. In \fBteam.points.add(...)\fR the list is lent to read while the argument reads it, and to change only once the argument is there. .SS "A tree that adds up to its root" .RS 4 .nf alias FolderRef = shared checked Folder struct Folder { string name long bytes weak checked Folder? parent List children FolderRef add(read FolderRef this, string name, long bytes) { var child = new FolderRef { name = name, bytes = bytes, parent = weak this, children = List() } this.children.add(share child) FolderRef? up = share this while up is not null { up.bytes += bytes up = share up.parent } return child } } void main() { var home = new FolderRef { name = "home".clone(), bytes = 0, parent = null, children = List() } var docs = home.add("docs".clone(), 10) var notes = docs.add("notes".clone(), 5) _ = home.add("music".clone(), 100) print($"{home.bytes} {docs.bytes} {notes.bytes}") for child in read home.children { print(child.name) } } .fi .RE .PP .RS 4 .nf 115 15 5 docs music .fi .RE .PP \fBread FolderRef this\fR borrows the owner, not the value, so the method can make \fBweak this\fR and \fBshare this\fR, and change the folder and each one above it. A folder owns its children and looks at its parent, so the tree holds no circle. .SS "A list that walks both ways" .RS 4 .nf struct Node { string word shared checked Node? next weak checked Node? prev } struct Line { shared checked Node? first weak checked Node? last void add(ref this, string word) { var tail = share this.last var node = new shared checked Node { word = word, next = null, prev = weak tail } if tail is null { this.first = share node } else { tail.next = share node } this.last = weak node } } void main() { var line = Line { first = null, last = null } for word in "the cat saw the dog".split(" ") { line.add(word.clone()) } var at = share line.first while at is not null { print(at.word) at = share at.next } var back = share line.last while back is not null { print(back.word) back = share back.prev } } .fi .RE .PP .RS 4 .nf the cat saw the dog dog the saw cat the .fi .RE .PP Each node owns the next and looks at the one before it. \fBtail.next = share node\fR changes a node that the list owns through another owner, \fBtail\fR\&. When \fBline\fR ends, the first node goes, then the next, down the line. .SH "WHAT STOPS THE PROGRAM" .SS "A change while a loop reads" .RS 4 .nf struct Queue { List jobs } void main() { var queue = new shared checked Queue { jobs = List { 1, 2, 3 } } var worker = share queue for job in read queue.jobs { if job == 2 { worker.jobs.add(4) } } } .fi .RE .PP .RS 4 .nf panic: this `Queue` is being read, so it cannot be changed here a `shared checked` value is lent to one that changes it or to any number that read it, not both at once --> example.sg:10:13 10 | worker.jobs.add(4) | ^^^^^^ it is being read from here: --> example.sg:8:21 8 | for job in read queue.jobs { | ^^^^^ .fi .RE .PP \fBqueue\fR and \fBworker\fR are two owners of one queue, so the compiler cannot tell. An \fBadd\fR could move the items the loop is reading; the check stops it first. .SS "Lent to change twice in one call" .RS 4 .nf struct Account { long balance } void transfer(ref Account from, ref Account to, long amount) { from.balance -= amount to.balance += amount } void main() { var alice = new shared checked Account { balance = 100 } var bob = new shared checked Account { balance = 50 } transfer(ref alice, ref bob, 30) print($"{alice.balance} {bob.balance}") var payee = share alice transfer(ref alice, ref payee, 10) } .fi .RE .PP .RS 4 .nf 70 80 panic: this `Account` is being changed, so it cannot be changed here a `shared checked` value is lent to one that changes it or to any number that read it, not both at once --> example.sg:16:25 16 | transfer(ref alice, ref payee, 10) | ^^^^^^^^^ it is being changed from here: --> example.sg:16:14 16 | transfer(ref alice, ref payee, 10) | ^^^^^^^^^ .fi .RE .PP The first transfer is between two accounts. The second would give one account to the call as two \fBref\fR borrows, which a \fBref\fR never shares. \fBtransfer(ref alice, ref alice, 10)\fR is refused by the compiler. .SS "A change on the way to what is lent" .RS 4 .nf struct Node { List items shared checked Node? next } void walk(read shared checked Node head, shared checked Node other) { match read head.next { null => print("end") var n => { for x in read n.items { other.next = null print(x) } } } } void main() { var tail = new shared checked Node { items = List { 1, 2, 3 }, next = null } var head = new shared checked Node { items = List(), next = tail } walk(read head, share head) } .fi .RE .PP .RS 4 .nf panic: this `Node` is being read, so it cannot be changed here a `shared checked` value is lent to one that changes it or to any number that read it, not both at once --> example.sg:11:17 11 | other.next = null | ^^^^^ it is being read from here: --> example.sg:7:16 7 | match read head.next { | ^^^^ .fi .RE .PP \fBhead.next\fR is the only owner of the second node. Setting it to \fBnull\fR through \fBother\fR would free the node whose items the loop reads, so the \fBmatch\fR holds \fBhead\fR too, not only the node it looks into. .SH "WHAT THE COMPILER REFUSES" .SS "A change the compiler can see" .RS 4 .nf struct Queue { List jobs } void main() { var queue = new shared checked Queue { jobs = List { 1, 2, 3 } } read queue as q { print(q.jobs.length()) queue.jobs.add(4) } } .fi .RE .PP .RS 4 .nf error: `queue` is being read for the whole block, so it cannot be changed here: this line would stop the program every time it ran a `shared checked` value is lent to one that changes it or to any number that read it, not both at once --> example.sg:9:9 9 | queue.jobs.add(4) | ^^^^^ it is being read from here: --> example.sg:7:10 7 | read queue as q { | ^^^^^ hint: open it with `ref queue as ...` and change it through that name .fi .RE .SS "A borrow kept past its line" .RS 4 .nf struct Queue { List jobs } void main() { var queue = new shared checked Queue { jobs = List { 1, 2, 3 } } var jobs = read queue.jobs print(jobs.length()) } .fi .RE .PP .RS 4 .nf error: this borrows what a `shared checked` value holds, and such a borrow ends with its line; `jobs` would keep it --> example.sg:7:16 7 | var jobs = read queue.jobs | ^^^^^^^^^^^^^^^ it is borrowed here: --> example.sg:7:21 7 | var jobs = read queue.jobs | ^^^^^ hint: lend it for a block instead: `read queue as value { var jobs = read value.jobs ... }` .fi .RE .PP A variable would look at the list while another owner changes or frees it. The block shows how long the borrow lasts, and the value is checked for all of it. .SS "checked alone" .RS 4 .nf struct Queue { checked List jobs } void main() { } .fi .RE .PP .RS 4 .nf error: `checked` is written after `shared` or `weak`: it is what makes a value with several owners changeable --> example.sg:2:5 2 | checked List jobs | ^^^^^^^ hint: `shared checked T` for an owner, `weak checked T` for a look at one .fi .RE .PP A field with one owner is changed through its struct; nothing about it needs checking while the program runs. .SS "Started as a task of its own" .RS 4 .nf struct Queue { List jobs } async nint fill(shared checked Queue queue) { await Task.delay(1) queue.jobs.add(4) return queue.jobs.length() } async void main() { var queue = new shared checked Queue { jobs = List() } var task = fill(share queue) print(await task) } .fi .RE .PP .RS 4 .nf error: `fill` takes a `shared checked Queue` here, and a call without `await` starts it as a task of its own, which this cannot go to: a `shared checked Queue` is checked within one task; one that several tasks change is `shared 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:13:21 13 | var task = fill(share queue) | ^^^^^^^^^^^ .fi .RE .PP \fBprint(await fill(share queue))\fR runs \fBfill\fR in this task, and is taken. .SH "SEE ALSO" .BR sg-shared (7), .BR sg-weak (7), .BR sg-share (7), .BR sg-as (7), .BR sg-locked (7), .BR sg-ref (7) .PP https://sg\-lang.org/reference/checked.html