Documentation channel: v0.0.5 at compiler commit 2c9c7e8. The latest tagged release is v0.0.5; pages identify APIs that are new in v0.0.5.
Async
Strut supports async functions, futures, await, and async lambdas. Async functions and async lambdas run on Strut's shared multithreaded executor. Independent ready tasks can execute concurrently across worker threads.
Async functions
Prefix a function declaration with async. Calling it returns a future for the declared result type.
async function answer() -> int {
return 42;
}
Awaiting a result
function main() -> int {
pending := answer();
value := await pending;
print(value);
return 0;
}
Async lambdas
Async lambdas use the same lambda syntax with an async prefix.
add := async (int x, int y) => x + y;
Async block lambdas
load := async (string path) => {
print("loading " + path);
return path;
};
Error propagation
Errors raised by asynchronous work are retained by the future and surface when the result is awaited. For an async function that declares checked errors, those errors are await-time effects that live in the returned future type:
async function fetch() -> int : HttpError {
return 1;
}
Calling fetch() does not itself require HttpError; the returned value has type future<int : HttpError>. Awaiting that future does require HttpError to be declared or caught:
function main() -> int {
pending := fetch(); // no HttpError required here
return 0;
}
function main() -> int : HttpError {
value := await fetch(); // HttpError declared in the signature
return value;
}
function main() -> int {
try {
value := await fetch();
} catch (HttpError err) {
value := 0;
}
return value;
}
Awaiting a future<T> with no declared checked errors requires nothing extra. An async function's own body follows normal checked-error discipline: awaiting an effectful future inside it requires that effect to be declared or caught there, and a declared effect becomes part of the async function's returned future<T : E>.
Executor behaviour
The executor owns a pool of native worker threads and wakes workers as tasks become ready. await retrieves a future result and rethrows asynchronous failures on the awaiting thread.
Long-running blocking work can occupy an executor worker, so explicitly blocking workloads should use Strut's thread/process facilities or dedicated blocking APIs rather than relying on async as a disguise for blocking work.
