11 ms·
Nim CPS: compile-time continuations
- disruptek 3y agoResubmit because I typo'd the link. XD
- zevv 3y agoSo, 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://without.boats/blog/let-futures-be-futures/ - https://adam.nels.onl//blog/maybe-everything-is-a-coroutine/ https://adam.nels.onl//blog/maybe-everything-is-a-coroutine/
- leorize 3y agoRight now I'm cooking up an stdlib based around CPS: https://github.com/alaviss/nim-sys 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.