Home / Docs / Diagnostics

U2023: A binding declared outside a together branch assigned, or passed as edit or take, inside it

Release v0.4.0

The branches of a together block, and the body of a together for, run as concurrent tasks (spec/19). A task captures the values of the bindings declared before it, as a lambda does, and cannot change them: inside a task, a binding declared outside it cannot be assigned, passed as edit, or passed as take (spec/19 §2 rule 3). Return the new value from the branch instead, and use it after the block, where every task has finished.

The repair at a glance

src/demo.unadiff
  module demo

  pub both(a: Int, b: Int) -> Int
  =
    let edit total = 0
    together
-     let x = bump(edit total, by: a)
+     let x = a * 2
      let y = b + 1
-   total + x + y
-
- bump(edit n: Int, by: Int) -> Int
- =
-   n = n + by
-   n
+   total = total + x
+   total + y

Failing example

una.tomltoml
[package]
name = "demo"
version = "0.1.0"
edition = "0.2"
src/demo.unauna
module demo

pub both(a: Int, b: Int) -> Int
=
  let edit total = 0
  together
    let x = bump(edit total, by: a)
    let y = b + 1
  total + x + y

bump(edit n: Int, by: Int) -> Int
=
  n = n + by
  n

Fixed example

una.tomltoml
[package]
name = "demo"
version = "0.1.0"
edition = "0.2"
src/demo.unauna
module demo

pub both(a: Int, b: Int) -> Int
=
  let edit total = 0
  together
    let x = a * 2
    let y = b + 1
  total = total + x
  total + y