diff options
Diffstat (limited to 'docs/ASYNC.md')
-rw-r--r-- | docs/ASYNC.md | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/docs/ASYNC.md b/docs/ASYNC.md index 5b9fa7e..ec610ca 100644 --- a/docs/ASYNC.md +++ b/docs/ASYNC.md @@ -1,17 +1,50 @@ # Asynchronous Programming -I don't know enough about asynchronous programming to get started with this section. +Puck has [colourless](https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/) async/await, heavily inspired by [Zig's implementation](https://kristoff.it/blog/zig-colorblind-async-await/). -Existing systems to learn from: -- https://github.com/status-im/nim-chronos -- https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/ -- https://tokio.rs/tokio/tutorial -- https://morestina.net/blog/1686/rust-async-is-colored -- https://ziglearn.org/chapter-5/ -- https://kristoff.it/blog/zig-colorblind-async-await/ -- https://en.wikipedia.org/wiki/Async/await -- https://old.reddit.com/r/elixir/np688d/ +```puck +pub func fetch(url: str): str = ... -Asynchronous programming is hard to design and hard to use. Even Rust doesn't do a great job. It *shouldn't* need built-in language support - we should be able to encode it as a type and provide any special syntax via macros. Note that async is not just threading! threading is solved well by Rust's rayon and Go's (blugh) goroutines. +let a: Future[T] = async fetch_html() +let b: T = a.await +let c: T = await async fetch_html() +``` -Is async worth having separate from effects? +Puck's async implementation relies heavily on its metaprogramming system. + +The `async` macro will wrap a call returning `T` in a `Future[T]` and compute it asynchronously. The `await` function takes in a `Future[T]` and will block until it returns a value (or error). The `Future[T]` type is opaque, containing internal information useful for the `async` and `await` routines. + +```puck +pub macro async(self): Future[T] = + ... todo ... +``` + +```puck +pub func await[T](self: Future[T]): T = + while not self.ready: + block + self.value! # apply callbacks? +``` + +This implementation differs from standard async/await implementations quite a bit. +In particular, this means there is no concept of an "async function" - any block of computation that resolves to a value can be made asynchronous. This allows for "anonymous" async functions, among other things. + +<!-- Asynchronous programming is hard to design and hard to use. Even Rust doesn't do a great job. It *shouldn't* need built-in language support - we should be able to encode it as a type and provide any special syntax via macros. Note that async is not just threading! threading is solved well by Rust's rayon and Go's (blugh) goroutines. --> + +## Threading + +How threads work deserves somewhat of a mention. todo + +References: +- [What color is your function?](https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/) +- [What is Zig's "colorblind" async/await?](https://kristoff.it/blog/zig-colorblind-async-await/) +- [Zig Learn: Async](https://ziglearn.org/chapter-5/) +- [Rust async is colored and that's not a big deal](https://morestina.net/blog/1686/rust-async-is-colored) +- [Why is there no need for async/await in Elixir?](https://old.reddit.com/r/elixir/np688d/) +- [Async/await on Wikipedia](https://en.wikipedia.org/wiki/Async/await) +- [nim-chronos](https://github.com/status-im/nim-chronos) +- [nim-cps](https://github.com/nim-works/cps) +- [tokio](https://tokio.rs/tokio/tutorial) +- [Zig-style async/await for Nim](https://forum.nim-lang.org/t/7347) + +Is async worth having separate from effect handlers? I think so... |