A task runs on its own heap: its arguments are copied into it, and it shares nothing with the
code that started it. A var parameter would let the task change a variable of its caller, so an
async fn that has one can only be awaited right away (which runs it in place, not as a task).
Wrong#
async fn fill(xs: var [int]) { xs.push(1) }
fn main() {
var xs: [int] = []
let t = fill(&xs)
await t
}Fixed#
async fn filled(xs: [int]): [int] = xs + [1]
fn main() {
let t = filled([])
let xs = await t
print(xs)
}Common causes#
- A worker that updates shared state: return the new value from the task, or send it on a
Chan. awaitdropped from a call such asawait answer(&conn): put it back.