Concurrent tasks: together
Release v0.4.0
together runs independent work at once with the result and trace of a sequential run (spec/19).
- A
togetherstatement holds two or moreletbranches (U2024 otherwise). Each branch runs as a task, sees the bindings before the block but not its siblings, and its binding is in scope after the block. Each branch's handling clauses stand on their own lines, as for anylet. let ys = together for x in xsruns its indented body once per element, or per integer of a range, and builds the list of results in order.- A task cannot assign,
editortakea binding declared outside it (U2023): return new values instead. Tasks use the capabilities of the enclosing function, and a parameter named byuses oforfails of. - When a task fails, the tasks after it are canceled, and the block fails with the failure of the first task that failed: the outcome of running the tasks one after another.
- The trace lists each task's events in task order, with the task's path, as
"task": "2"; the order in which tasks really performed effects, such as the lines they print, may differ.
una
module demo
pub kind ReadError = | Unreachable
pub capability Sensor
op reading() -> Int
fails ReadError
pub warmest() -> Int
uses @north: Sensor[reading]
uses @south: Sensor[reading]
fails ReadError
=
together
let n = @north.reading()
if error pass
let s = @south.reading()
if error pass
max(n, b: s)
pub squares(n: Int) -> List[Int]
=
let squares = together for i in 0..<n
i * i
squaresuna
-- fails with U2023
module demo
pub count(xs: List[Int]) -> Int
=
let edit seen = 0
let doubled = together for x in xs
seen = seen + 1
x * 2
seen