koto-lang / koto

A simple, expressive, embeddable programming language, made with Rust
https://koto.dev
MIT License
515 stars 27 forks source link

Add support for async tasks #277

Open irh opened 8 months ago

irh commented 8 months ago

It would be good to have async functions in Koto, and I think that it would be a natural extension of the existing Generator concept to add support for async workflows.

Generator functions work by creating an iterator when first called, which contains a VM that's responsible for executing the function, suspending execution each time yield is encountered. Each time .next() is called the iterator runs until the next yield expression, or the function exits.

I think generators could be replaced with a new Task value type that supports an await expression alongside the existing yield.

await would suspend execution of the task while waiting for output from some other task. The task's VM would either block on the awaited result becoming available, or it could be managed by an async runtime that monitors the progress of multiple parallel tasks.

So, the new Task type will be able to:

irh commented 7 months ago

Some groundwork for this was implemented in #291 - the VM now has an execution_state field which has a Suspended variant, used when a generator has just yielded a value.

Additionally, continue_running now returns a ReturnOrYield enum that lets the caller know the nature of the resulting value.

ReturnOrYield could be extended (and renamed) to include an Await variant that passes the task that's being waited on to an executor (or something 🙃).