Error codes

E0336: an async fn with a var parameter started as a task

In a terminal: auk explain E0336 prints this page.

On this page

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#

auk
async fn fill(xs: var [int]) { xs.push(1) }

fn main() {
  var xs: [int] = []
  let t = fill(&xs)
  await t
}

Fixed#

auk
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.
  • await dropped from a call such as await answer(&conn): put it back.

Stuck on a word? Try the glossary. Got a compiler error? Look up its code. Improve this page