.\" Made by website/plain/build.mjs from website/words/as.md; change that file. .TH SG-AS 7 2026-11-28 "SG" "SG reference" .SH NAME as \- names a value lent for a block: a shared value opened, a locked value taken, an item a select receives .SH SYNOPSIS .TP \fBread x as name { ... }\fR opens a shared or shared checked value to read, for the block .TP \fBref x as name { ... }\fR opens a shared checked value to change, for the block .TP \fBlock x as name { ... }\fR takes a shared locked value; name changes it for the block .TP \fBlock a as x, b as y { ... }\fR takes two locked values at once, in a fixed order .TP \fBlock x as name when condition { ... }\fR waits, with the lock let go, until the condition holds .TP \fBawait lock x as name when condition { ... }\fR the same in an async function .TP \fBch.receive() as name => ...\fR in a select, the arm that receives names its item; as _ drops it .SH DESCRIPTION \fBas\fR gives a name to a value for the length of a block. The name borrows: inside the block the value is not moved or assigned, and at the closing \fB}\fR the borrow ends. The block shows in the code how long the borrow lasts. .PP \fBread x as name\fR and \fBref x as name\fR open a value with several owners. Each access to a \fBshared checked\fR value is checked while the program runs, and a borrow of what it holds ends with its line, so a variable cannot keep one. A loop over one of its lists, or a result that borrows, such as \fBtable.getOrAdd(word, 0)\fR, is written inside the block, where the borrow lasts to the \fB}\fR\&. \fBread ... as\fR also opens a plain \fBshared\fR value; \fBref ... as\fR does not, since a plain \fBshared\fR value is only read. Any other value, a \fBT?\fR and a \fBweak\fR value are errors. .PP \fBlock x as name\fR takes a \fBshared locked\fR value, or a \fBstatic locked\fR one, and \fBname\fR is a \fBref\fR to what it holds for the block. Only one task is inside a \fBlock\fR block of the value at a time. With \fBwhen\fR, the task waits until the condition, which sees the names as \fBread\fR, holds. A \fBlock\fR block itself never waits: no other \fBlock\fR, no \fBwait\fR and no channel inside it, so locks cannot deadlock. .PP In a \fBselect\fR, \fBreceive() as name\fR names the item an arm receives; the item is owned by the arm. .PP There is no cast with \fBas\fR: a cast is written \fB(byte)total\fR\&. And \fBuse\fR gives a module no second name. .SH EXAMPLES .SS "Reading a shared value for a block" .RS 4 .nf struct Account { string owner List history } void main() { var mine = new shared checked Account { owner = "Ada".clone(), history = List { 50, -20, 75 } } read mine as account { long total = 0 for amount in read account.history { total += amount } print($"{account.owner}: {total} over {account.history.length()} entries") } } .fi .RE .PP .RS 4 .nf Ada: 105 over 3 entries .fi .RE .PP \fBaccount\fR reads the value for the whole block, so the \fBfor\fR can walk its list. While the block runs, no owner of \fBmine\fR can change it. .SS "A borrow kept past its line" .RS 4 .nf void main() { var counts = new shared checked HashMap() var words = "the cat saw the dog and the bird".split(" ") for word in words { ref counts as table { var slot = table.getOrAdd(word, 0) slot += 1 } } read counts as table { print(table["the"]) print(table.length()) } } .fi .RE .PP .RS 4 .nf 3 6 .fi .RE .PP \fBgetOrAdd\fR gives a borrow of a count inside the map. Outside a block it would end with its line; inside \fBref counts as table\fR it lasts as long as \fBslot\fR is used. .SS "Several tasks change one value" .RS 4 .nf void record(shared locked int visits, int count) { for _ in 0..count { lock visits as v { v += 1 } } } void main() { var visits = new shared locked 0 var home = Task.run(record, share visits, 300) var shop = Task.run(record, share visits, 200) home.wait() shop.wait() lock visits as v { print($"{v} visits") } } .fi .RE .PP .RS 4 .nf 500 visits .fi .RE .PP \fBv\fR is the number itself, lent to change. Each addition is made by one task at a time, so none is lost. .SS "Two values, and a condition" .RS 4 .nf struct Stock { int count } struct Orders { int filled } void sell(shared locked Stock stock, shared locked Orders orders, int amount) { lock stock as s, orders as o when s.count >= amount { s.count -= amount o.filled += 1 } } void main() { var stock = new shared locked Stock { count = 0 } var orders = new shared locked Orders { filled = 0 } var first = Task.run(sell, share stock, share orders, 3) var second = Task.run(sell, share stock, share orders, 4) for _ in 0..10 { lock stock as s { s.count += 1 } } first.wait() second.wait() lock stock as s, orders as o { print($"{o.filled} orders filled, {s.count} left") } } .fi .RE .PP .RS 4 .nf 2 orders filled, 3 left .fi .RE .PP Each seller waits, with both locks let go, until there is enough stock. The condition is looked at again each time a \fBlock\fR block on one of the values ends, so the loop in \fBmain\fR wakes them without a word more. .SS "Naming what a select receives" .RS 4 .nf void produce(Sender out, string name, int count) { for i in 0..count { _ = out.send($"{name} {i}".toString()) } } void main() { var orders = Channel(2) var alerts = Channel(2) var a = Task.run(produce, orders.sender(), "order", 3) var b = Task.run(produce, alerts.sender(), "alert", 2) var seen = 0 var alertCount = 0 while true { select { orders.receive() as _ => seen += 1 alerts.receive() as alert => { alertCount += 1 print(alert) } ended => break } } a.wait() b.wait() print($"{seen} orders, {alertCount} alerts") } .fi .RE .PP .RS 4 .nf alert 0 alert 1 3 orders, 2 alerts .fi .RE .PP \fBalert\fR is the text the arm received. \fBas _\fR takes an order and drops it, since only the count is wanted. .SH "WHAT STOPS THE PROGRAM" .SS "A change while the block reads" .RS 4 .nf struct Account { long balance } void main() { var mine = new shared checked Account { balance = 100 } var bank = share mine read mine as account { bank.balance -= 30 print(account.balance) } } .fi .RE .PP .RS 4 .nf panic: this `Account` 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:9:9 9 | bank.balance -= 30 | ^^^^ it is being read from here: --> example.sg:8:10 8 | read mine as account { | ^^^^ .fi .RE .PP \fBbank\fR is another owner of the same account, which the checker does not follow; the check while the program runs finds it, in the interpreter and in every compiled build. .SS "One value locked twice" .RS 4 .nf struct Account { long balance } void transfer(shared locked Account from, shared locked Account to, long amount) { lock from as a, to as b { a.balance -= amount b.balance += amount } } void main() { var alice = new shared locked Account { balance = 100 } transfer(share alice, share alice, 10) } .fi .RE .PP .RS 4 .nf panic: `a` and `b` are one value, which a `lock` takes once --> example.sg:6:21 6 | lock from as a, to as b { | ^^ .fi .RE .PP Two names would change one value at once. A transfer to oneself is tested for before the \fBlock\fR\&. .SH "WHAT THE COMPILER REFUSES" .SS "A value that is not shared" .RS 4 .nf void main() { var scores = List { 3, 5 } read scores as view { print(view.length()) } } .fi .RE .PP .RS 4 .nf error: `read ... as` opens a `shared` value for a block, and this is `List` --> example.sg:3:10 3 | read scores as view { | ^^^^^^ hint: use `scores` as it is .fi .RE .PP A value with one owner is borrowed with \fBread scores\fR where it is needed, or \fBvar view = read scores\fR\&. .SS "A borrow of a checked value kept in a variable" .RS 4 .nf void main() { var counts = new shared checked HashMap() var slot = counts.getOrAdd("tea", 0) slot += 1 } .fi .RE .PP .RS 4 .nf error: this borrows what a `shared checked` value holds, and such a borrow ends with its line; `slot` would keep it --> example.sg:3:16 3 | var slot = counts.getOrAdd("tea", 0) | ^^^^^^^^^^^^^^^^^^^^^^^^^ it is borrowed here: --> example.sg:3:16 3 | var slot = counts.getOrAdd("tea", 0) | ^^^^^^ hint: lend it for a block instead: `ref counts as value { var slot = value.getOrAdd("tea", 0) ... }` .fi .RE .PP The block is \fBref\fR, since \fBgetOrAdd\fR and \fBslot += 1\fR change the map; inside it, \fBslot\fR keeps the borrow to the \fB}\fR\&. .SS "A lock without a name" .RS 4 .nf void main() { var hits = new shared locked 0 lock hits { hits += 1 } } .fi .RE .PP .RS 4 .nf error: expected `as` and a name for what is locked, found `{` --> example.sg:3:15 3 | lock hits { | ^ hint: `lock total as c { c += 1 }` .fi .RE .SS "A received item with no name" .RS 4 .nf void produce(Sender out) { _ = out.send(1) } void main() { var numbers = Channel(2) var task = Task.run(produce, numbers.sender()) select { numbers.receive() => print("got one") ended => print("none") } task.wait() } .fi .RE .PP .RS 4 .nf error: the item an arm of a `select` receives is given a name --> example.sg:9:9 9 | numbers.receive() => print("got one") | ^^^^^^^^^^^^^^^^^ hint: `numbers.receive() as item => ...`, or `numbers.receive() as _ => ...` to drop it .fi .RE .SH "SEE ALSO" .BR sg-read (7), .BR sg-ref (7), .BR sg-lock (7), .BR sg-locked (7), .BR sg-checked (7), .BR sg-shared (7) .PP https://sg\-lang.org/reference/as.html