Colorless Concurrency
Structured concurrency without async/await keywords: Async.scope, fail-fast cancellation, row-mapped Async.all, Semaphore, Channel, sync specialization, and deterministic virtual-clock testing.
Concurrency without function coloring
Exp has no async or await syntax. Calling any suspending operation
simply adds Async to the function's inferred effect row. Higher-order functions
like List.map, applyTwice, and Async.retry are
effect-polymorphic over an open row variable e — you write them once and call them
from both synchronous and suspending code without duplicating APIs.
Async.scope & fail-fast sibling cancellation
Async.scope do ... end introduces a lexical Spawn<E> capability
enforcing strict structured concurrency:
- Every spawned fiber's error row merges into
E. - If any child fiber or the scope body fails, all sibling fibers are immediately cancelled and run their LIFO
deferand affine cleanups. fiber.join()never fails directly — errors surface deterministically at the scope boundary with their exact typed error union.- Nothing outlives its enclosing scope; spawning a fallible fiber outside an
Async.scopeis a compile error.
type FetchErr
Transient
end
fn fetchDashboard()
Async.scope do
attempt = Ref.new(0)
Async.all({
user: =>
Async.sleep(10.ms())
{ id: 1, name: "Ada" }
,
posts: => Async.retry(attempts: 3) do
attempt.set(attempt.get() + 1)
if attempt.get() < 2 then fail(Transient) end
["Algebraic Effects", "Zero-Cost DI"]
end,
alerts: => Async.timeout(50.ms(), =>
Async.sleep(200.ms())
["never arrives"]
),
})
end
end
fn main()
res = handle fetchDashboard() with
fn Fail.raise(Transient) { user: { id: 0, name: "err" }, posts: [], alerts: None } end
end
alertLabel = match res.alerts with
Some(xs) then "{xs.len()}"
None then "timed out (None)"
end
print("user={res.user.name}, posts={res.posts.len()}, alerts={alertLabel}")
endError-polymorphic concurrency combinators
| Combinator | Behavior |
|---|---|
Async.all([=> a(), => b()]) | Runs thunks concurrently inside a scope; cancels siblings on first failure. |
Async.all({ user: => ..., posts: => ... }) | Row-mapped concurrent record evaluation preserving every field's exact type via Row.map. |
Async.race([=> primary(), => backup()]) | Returns first completion and deterministically cancels all losing fibers. |
Async.timeout(500.ms(), => work()) | Returns Option<T> (None on deadline) while cancelling timed-out work cleanly. |
Async.retry(attempts: 3) do ... end | Error-polymorphic retry preserving the exact union Fail<U>. |
Async.parallel(items, limit: 4) do |x| ... end | Bounded concurrency preserving input order and fail-fast cancellation. |
Semaphore.new(n) / gate.hold do ... end | Scoped permit acquisition guaranteeing release on failure or cancellation. |
Channel.new() / Channel.bounded(n) | Typed concurrent channels supporting send, receive, close, and for msg in ch do. |
Tier 4 sync/async specialization (B6)
When an effect-polymorphic helper is instantiated without Async in its concrete
effect row, the JavaScript backend specializes it to a plain synchronous JS function with
0 Promise allocations and 0 microtask ticks.
Inspect the JavaScript tab below to verify there is zero async
keyword or wrapper emitted:
--- Effect-polymorphic helper: works with BOTH pure and Async callbacks.
fn applyTwice(x: Int, f)
f(f(x))
end
fn main()
-- Synchronous instantiation: 0 Promises, 0 async keywords, 0 microtask ticks!
syncResult = applyTwice(3, n => n * 2)
print("sync={syncResult}")
end
Hover or focus any symbol to inspect inferred types, effects, and errors Deterministic virtual clock in exp test
exp test (and exp build --virtual-clock) runs timers on a
deterministic virtual clock that advances instantaneously to the next timer deadline. Tests
involving Async.sleep, Async.timeout, and Async.race
execute in sub-millisecond time with zero flakiness.
test("timeout returns None instantaneously on virtual clock") do
result = Async.timeout(100.ms(), =>
Async.sleep(500.ms())
42
)
assertEq(result, None)
end
test("race returns fastest fiber and cancels loser") do
winner = Async.race([
=>
Async.sleep(250.ms())
"slow"
,
=>
Async.sleep(20.ms())
"fast"
,
])
assertEq(winner, "fast")
end
Hover or focus any symbol to inspect inferred types, effects, and errors