r/ProgrammingLanguages • u/andeee23 • Aug 06 '26
Why spawning work isn’t `async` in my language
I’m designing a native language with a closed set of five effects: async, throws, io, alloc, and task.
It uses the effect system to determine which functions can run in which context.
A gpu function should be pure so it can be compiled to gpu shader code, comptime and macros don't allow io, etc.
The unusual one is task. Starting, polling, or cancelling independent work does not necessarily suspend the caller, so it distinguishes interacting with a task from suspending the current control flow:
fn download(url: str): Data !{async, io, alloc, throws(NetworkError)} {
// This function may suspend and fail.
try await http.get(url)
}
fn begin_download(scope: mut TaskScope, url: str): Job[Data, NetworkError]
!{task, io, alloc} {
// Starts independent work, but does not suspend or throw here.
scope.start(() => download(url))
}
fn finish_download(job: Job[Data, NetworkError]): Data !{task, async, throws(NetworkError)} {
// This is where the current control flow may suspend
// and where the job's result or error is observed.
try await job
}In other words:
asyncmeans this control flow may suspend.taskmeans this code interacts with independent task or executor state.
Why is task needed at all? Without it this function would appear pure:
fn surprise(scope: mut TaskScope) {
// Returns immediately, but schedules a later mutation.
scope.start(() => cache.clear())
}That would make it legal to run during compile-time evaluation, a reactive computation, or any context allowed to repeat or discard “pure” work.
async cannot express this because the caller does not suspend. io cannot express it because the executor and affected state may be entirely internal. alloc cannot express it because scheduling and cancellation are observable even when allocation is optimized away.