.\" Made by website/plain/build.mjs from website/words/share.md; change that file. .TH SG-SHARE 7 2026-11-28 "SG" "SG reference" .SH NAME share \- makes one more owner of a shared value, or an owner from a weak look .SH SYNOPSIS .TP \fBshare x\fR one more owner of the value x points to; x stays an owner .TP \fBshare w\fR an owner from a weak look, or null once the value is gone .TP \fBshare x, with x null\fR null: the result of share on a T? is a T? .TP \fBshare this\fR in a method that takes read shared T this: an owner of the value it was called on .TP \fBx.clone()\fR of a shared T: the same as share x, nothing is copied .SH DESCRIPTION A \fBshared T\fR is one owner of a value on the heap, made by \fBnew shared x\fR\&. Given to a variable, a field or a call, it moves, as any owner does, and the place it left can no longer be used. \fBshare x\fR makes one more owner instead: both point to the same value, nothing is copied, and \fBx\fR goes on as before. .PP Each \fBshare\fR adds one to the count of owners, and each owner that ends takes one away. When the last one ends the value is let go: its \fBdispose\fR runs, if it has one, and its memory is freed. The count is atomic, so an owner made by \fBshare\fR can go to another task. No method reads it: a program asks \fBsame(a, b)\fR, or holds a \fBweak\fR look and asks \fBshare\fR whether the value is still there. .PP \fBshare\fR on a \fBweak\fR look gives a \fBshared T?\fR: an owner while the value is alive, \fBnull\fR once it is gone. On a \fBshared T?\fR that is \fBnull\fR, \fBshare\fR gives \fBnull\fR too, so a value that may be missing needs no test before it. .PP \fBshare\fR costs an atomic step, and so does the end of each owner. A function that only reads the value takes a \fBread\fR borrow, which does not touch the count; \fBshare\fR is written where a second owner is kept: in a field, in a collection, or in another task. It works the same on \fBshared checked\fR and \fBshared locked\fR values. .SH EXAMPLES .SS "One more owner" .RS 4 .nf struct Theme { string name string accent } void main() { var theme = new shared Theme { name = "dark", accent = "teal" } var editor = share theme var terminal = share theme print($"{editor.name} {terminal.accent}") print($"{theme.name} is still here") print(same(editor, terminal)) } .fi .RE .PP .RS 4 .nf dark teal dark is still here true .fi .RE .PP Three owners point to one \fBTheme\fR\&. \fBsame\fR tells that two of them point to the same value. .SS "Let go with the last owner" .RS 4 .nf struct Connection : Disposable { string host void dispose(ref this) { print($"closed {this.host}") } } void main() { var first = new shared Connection { host = "db.local" } var second = share first print("two owners") first = new shared Connection { host = "cache.local" } print("one owner left") second = share first print("none left for db.local") } .fi .RE .PP .RS 4 .nf two owners one owner left closed db.local none left for db.local closed cache.local .fi .RE .PP When \fBfirst\fR gets a new value, \fBsecond\fR still owns \fBdb.local\fR\&. Once \fBsecond\fR is given an owner of the new one, nothing owns \fBdb.local\fR and its \fBdispose\fR runs; \fBcache.local\fR is let go when \fBmain\fR ends. .SS "An owner from inside a method" .RS 4 .nf struct Team { string name Member join(read shared Team this, string person) { return Member { name = person, team = share this } } } struct Member { string name shared Team team } void main() { var team = new shared Team { name = "compiler" } var ada = team.join("Ada") var linus = team.join("Linus") print($"{ada.name} and {linus.name} are in {ada.team.name}") print(same(ada.team, linus.team)) } .fi .RE .PP .RS 4 .nf Ada and Linus are in compiler true .fi .RE .PP \fBread shared Team this\fR borrows the owner, not only the \fBTeam\fR, so the method can make one more owner of the value it was called on. .SS "An owner that may be missing" .RS 4 .nf struct Image { string path int width } void main() { var cache = HashMap() cache.add("logo", new shared Image { path = "logo.png", width = 64 }) var logo = share cache.get("logo") var banner = share cache.get("banner") if logo is not null { print($"{logo.path} {logo.width}") } print(banner is null) } .fi .RE .PP .RS 4 .nf logo.png 64 true .fi .RE .PP \fBcache.get\fR gives \fBnull\fR for a key it does not have, and \fBshare\fR of \fBnull\fR is \fBnull\fR\&. Each \fBshare\fR here gives a \fBshared Image?\fR, which is tested before it is used. .SS "Walking up through weak links" .RS 4 .nf struct Folder { string name weak Folder? parent } string pathOf(read Folder folder) { var path = folder.name.clone() var at = share folder.parent while at is not null { path = $"{at.name}/{path}".toString() at = share at.parent } return path } void main() { var home = new shared Folder { name = "home", parent = null } var docs = new shared Folder { name = "docs", parent = weak home } var notes = new shared Folder { name = "notes", parent = weak docs } print(pathOf(read notes)) docs = new shared Folder { name = "papers", parent = weak home } print(pathOf(read notes)) print(pathOf(read docs)) } .fi .RE .PP .RS 4 .nf home/docs/notes notes home/papers .fi .RE .PP A folder looks at its parent through a \fBweak\fR link, which does not keep it alive. Once \fBdocs\fR is given a new folder, the old one has no owner; \fBshare\fR on the link of \fBnotes\fR then gives \fBnull\fR, and the walk stops there. .SS "Owners in other tasks" .RS 4 .nf struct Prices { List cents } long total(shared Prices prices) { long sum = 0 for c in read prices.cents { sum += c } return sum } long highest(shared Prices prices) { long top = 0 for c in read prices.cents { top = top.max(c) } return top } void main() { var prices = new shared Prices { cents = List { 1250, 399, 7800, 15 } } var sum = Task.run(total, share prices) var top = Task.run(highest, share prices) print($"total {sum.wait()}, highest {top.wait()}") print(prices.cents.length()) } .fi .RE .PP .RS 4 .nf total 9464, highest 7800 4 .fi .RE .PP Each task gets an owner of its own and reads the same list, which is not copied. The prices are let go when the last of the three owners ends: here \fBprices\fR, at the end of \fBmain\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "An owner given away without share" .RS 4 .nf struct Theme { string name } void main() { var theme = new shared Theme { name = "dark" } var editor = theme print(editor.name) print(theme.name) } .fi .RE .PP .RS 4 .nf error: `theme` was moved and can no longer be used --> example.sg:9:11 9 | print(theme.name) | ^^^^^ it was moved here: --> example.sg:7:18 7 | var editor = theme | ^^^^^ .fi .RE .PP A \fBshared\fR owner moves like any other value. \fBvar editor = share theme\fR keeps both. .SS "share of a value with one owner" .RS 4 .nf struct Theme { string name } void main() { var theme = Theme { name = "dark" } var editor = share theme print(editor.name) } .fi .RE .PP .RS 4 .nf error: `share` needs a `shared` value or a `weak` one, and this is `Theme` --> example.sg:7:24 7 | var editor = share theme | ^^^^^ hint: a value with several owners is made with `new shared`: `var owner = new shared Node { ... }` .fi .RE .SS "An owner from a weak look, used without a test" .RS 4 .nf struct Theme { string name } void main() { var theme = new shared Theme { name = "dark" } var look = weak theme var other = share look print(other.name) } .fi .RE .PP .RS 4 .nf error: this is `shared Theme?` and may be `null`; `.name` needs a value --> example.sg:9:11 9 | print(other.name) | ^^^^^ hint: test it first: `if other is not null { ... }` .fi .RE .SS "A shared checked owner sent to a task" .RS 4 .nf struct Account { long balance } long balanceOf(shared checked Account account) { return account.balance } void main() { var account = new shared checked Account { balance = 10 } var task = Task.run(balanceOf, share account) print(task.wait()) } .fi .RE .PP .RS 4 .nf error: `balanceOf` takes a `shared checked Account` here, which cannot go to another task: a `shared checked Account` is checked within one task; one that several tasks change is `shared locked` .fi .RE .PP A plain \fBshared\fR value is only read, so its owners may be in several tasks. A \fBshared checked\fR one is changed through its owners, and its checks hold within one task. .SH "SEE ALSO" .BR sg-shared (7), .BR sg-weak (7), .BR sg-new (7), .BR sg-checked (7), .BR sg-locked (7) .PP https://sg\-lang.org/reference/share.html