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.
6
u/initial-algebra Aug 06 '26
Isn't the
mut TaskScopeparameter enough? Alternatively, why do we need amut TaskScopeinstead of just using thetaskeffect?3
u/Silly-Freak Aug 06 '26
Would there be a way to get rid of this effect? For example, you don't need to declare
throws, even when you call a function that does, if youcatch.My feeling is that creating the
TaskScopeis already that. Assuming this is structured concurrency, tasks don't escape their scope, so holding the task scope object both marks the effect, and gives you a way to terminate it, no?1
u/Vovandosy Aug 06 '26
isn't
taskhere closer to coeffects rather than to effects by the way?