Settings

Theme

Nim CPS: compile-time continuations

github.com

7 points by disruptek 2 years ago · 3 comments

Reader

leorize 2 years ago

Right now I'm cooking up an stdlib based around CPS: https://github.com/alaviss/nim-sys

The major perk is that you queue computation (read: "thread"), not callbacks, so you can write stuff like this:

    while true:
      let bytesRead = read(fd, buf, bufsize)
      if bytesRead == -1:
        if errno == EAGAIN or errno == EWOULDBLOCK:
          wait(fd, Read) # Queue this computation until the fd is readable
        else:
          raise newIOError(errno)
      else:
        return bytesRead
Even IOCP is made simple here:

    let overlapped = new Overlapped
    # <do your operation>
    if error == WSA_IO_PENDING:
      wait(handle, overlapped) # Queue this computation until the overlapped operation is complete
      if WSAGetOverlappedResult(...) == FALSE:
        raise newIOError(errno)
      else: ...
I think this kind of simplicity afforded to user code is rarely seen in most languages, which is why I decided to base my stdlib on this.
zevv 2 years ago

So, what this is: it's a Nim macro that rewrites your regular procedural code into a continuation using the CPS transform; this offers you total power over the flow control of your code and allows you to build things like coroutines, async I/O schedulers, iterators, exceptions, all as first class citizens in the language. It has been under development for quite some time and is pretty much mature.

Relevant recent articles:

- https://without.boats/blog/let-futures-be-futures/

- https://adam.nels.onl//blog/maybe-everything-is-a-coroutine/

disruptekOP 2 years ago

Resubmit because I typo'd the link. XD

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection