.\" Made by website/plain/build.mjs from website/words/shared.md; change that file. .TH SG-SHARED 7 2026-11-28 "SG" "SG reference" .SH NAME shared \- shared T: a value on the heap with several owners, only read .SH SYNOPSIS .TP \fBnew shared x\fR puts x on the heap and gives its first owner .TP \fBshare x\fR one more owner of the same value .TP \fBshared T, shared T?\fR the type of an owner, one that may be missing .TP \fBweak x\fR a look that does not keep the value alive .TP \fBshare w\fR an owner from a weak look, or null once the value is gone .TP \fBsame(a, b)\fR whether two owners point to one value .TP \fBx.makeUnique()\fR a ref T to change: the value itself, or a copy of it .TP \fBshared checked T\fR changed through any owner, every access checked .TP \fBshared locked T\fR changed by tasks, inside lock .SH DESCRIPTION Most values have one owner. Some have none that is clearly first: one configuration read by several servers, the nodes of a graph. \fBnew shared x\fR puts the value on the heap, and \fBshare x\fR makes one more owner of it, without a copy. The value is let go when its last owner ends. .PP What a \fBshared T\fR points to is only read. Any owner could change it under a borrow another owner holds, and the compiler cannot see all the owners. A value changed through several owners is a \fBshared checked T\fR, whose every access is checked while the program runs, or a \fBshared locked T\fR, which tasks change one at a time. .PP The count of owners is atomic, so an owner can go to another task. It costs on \fBshare\fR and where an owner ends; a borrow of a \fBshared\fR value does not touch it. .PP Owners that point at each other in a circle keep each other alive. The side that does not own is made \fBweak\fR; the interpreter, the tests and a debug build report such a circle when the program ends. .SH EXAMPLES .SS "Two owners of one value" .RS 4 .nf struct Config { string host int port } struct Server { string name shared Config config } void main() { var config = new shared Config { host = "localhost", port = 8080 } var api = Server { name = "api", config = share config } var admin = Server { name = "admin", config = share config } print($"{api.name} {api.config.host}:{api.config.port}") print($"{admin.name} {admin.config.host}:{admin.config.port}") print(same(api.config, admin.config)) } .fi .RE .PP .RS 4 .nf api localhost:8080 admin localhost:8080 true .fi .RE .PP Both servers own the one configuration; it is let go after the last of them ends. .SS "A look that does not keep it alive" .RS 4 .nf struct Config { string host } void show(read weak Config look) { var owner = share look if owner is null { print("gone") } else { print(owner.host) } } void main() { var config = new shared Config { host = "localhost" } var look = weak config show(read look) config = new shared Config { host = "example.org" } show(read look) } .fi .RE .PP .RS 4 .nf localhost gone .fi .RE .PP When \fBconfig\fR gets a new value, the old one has no owner left. \fBshare look\fR then gives \fBnull\fR\&. .SS "Changed by copying first" .RS 4 .nf struct Config : Clonable { string host int port } void main() { var config = new shared Config { host = "localhost", port = 8080 } var alone = config.makeUnique() alone.port = 9090 print($"{config.host}:{config.port}") var other = share config var mine = config.makeUnique() mine.host = "example.org" print($"{config.host}:{config.port} {other.host}:{other.port} {same(config, other)}") } .fi .RE .PP .RS 4 .nf localhost:9090 example.org:9090 localhost:9090 false .fi .RE .PP While \fBconfig\fR is the only owner, \fBmakeUnique\fR changes the value where it lies. Once \fBother\fR shares it, \fBconfig\fR gets a copy of its own first, and \fBother\fR keeps what it had. .SS "Changed through any owner" .RS 4 .nf struct Account { long balance List history void deposit(ref this, long amount) { this.balance += amount this.history.add(amount) } } void main() { var mine = new shared checked Account { balance = 0, history = List() } var bank = share mine mine.deposit(50) bank.deposit(20) print($"{mine.balance} {mine.history}") } .fi .RE .PP .RS 4 .nf 70 [50, 20] .fi .RE .PP A \fBshared checked\fR value is lent to one that changes it or to any number that read it, never both at once. .SS "Owners in two tasks" .RS 4 .nf struct Table { List words } long countLong(shared Table table, nint from, nint to) { long n = 0 for i in from..to { if table.words[i].length() > 4 { n += 1 } } return n } void main() { var words = List() for w in "shared values go to other tasks without a copy".splitWhitespace() { words.add(w.clone()) } var table = new shared Table { words = words } var half = table.words.length() / 2 var low = Task.run(countLong, share table, 0, half) var high = Task.run(countLong, share table, half, table.words.length()) print(low.wait() + high.wait()) } .fi .RE .PP .RS 4 .nf 5 .fi .RE .PP Each task owns the table while it runs, and neither copies the words. .SH "WHAT STOPS THE PROGRAM" .SS "A shared checked value lent twice" .RS 4 .nf struct Account { long balance void addFrom(ref this, read shared checked Account other) { this.balance += other.balance } } void main() { var mine = new shared checked Account { balance = 10 } var alias = share mine mine.addFrom(read alias) } .fi .RE .PP .RS 4 .nf panic: this `Account` is being changed, so it cannot be read 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:5:25 5 | this.balance += other.balance | ^^^^^ it is being changed from here: --> example.sg:12:5 12 | mine.addFrom(read alias) | ^^^^ .fi .RE .PP \fBmine\fR and \fBalias\fR are two owners of one account, so the compiler cannot tell; the check finds it when it happens, in every build. .SH "WHAT THE COMPILER REFUSES" .SS "Writing through a shared value" .RS 4 .nf struct Config { string host int port } void main() { var config = new shared Config { host = "localhost", port = 8080 } var other = share config other.port = 9090 print(config.port) } .fi .RE .PP .RS 4 .nf error: this field is reached through a `shared` value, which is only read and cannot be written a `shared` value has several owners, and none of them changes it alone; a value that is changed through several owners is a `shared checked T`, whose every access is checked against the borrows of it in progress --> example.sg:9:5 9 | other.port = 9090 | ^^^^^^^^^^ .fi .RE .SS "Reaching through a weak look" .RS 4 .nf struct City { int id } void main() { var home = new shared City { id = 1 } var look = weak home print(look.id) } .fi .RE .PP .RS 4 .nf error: this is `weak City`: what it looks at may be gone, so `.id` cannot reach it --> example.sg:8:11 8 | print(look.id) | ^^^^ hint: ask for an owner first: `var owner = share look`, which is `null` if it is gone .fi .RE .SS "A shared value as a key" .RS 4 .nf struct City { int id } void main() { var seen = HashSet() _ = seen.add(new shared City { id = 1 }) } .fi .RE .PP .RS 4 .nf error: `shared City` cannot be an item of a `HashSet`: it is not `Hashable` --> example.sg:6:24 6 | var seen = HashSet() | ^^^^^^^^^^^ hint: keep something that tells the values apart instead, such as an `id` field: `HashSet` .fi .RE .PP What it points to may change through \fBchecked\fR, and a key that changes breaks the table. .SH "SEE ALSO" .BR sg-weak (7), .BR sg-share (7), .BR sg-checked (7), .BR sg-locked (7), .BR sg-new (7) .PP https://sg\-lang.org/reference/shared.html