Continuation Passing Style

The actual implementation of suspending computations is done using CPS. This paradigm is based on the premise of sending a continuation to a function that is invoked, so that upon completion, the function will invoke the continuation. You can think of continuations as callbacks: whenever a suspending computation invokes another, it will pass a continuation that should be called upon completion or error.

All the heavy lifting is done by the compiler, which transforms all the suspending computations so that they send and receive said continuations—as we will see, this means that the actual signatures of suspending functions are not the same as what we define. On top of that, the suspending computations are transformed into state machines that can save and restore their state, and execute one portion of their code at a time—so whenever they are resumed, they restore their state and continue execution where they left off.

Coupling CPS with state machines, the compiler creates computations that can be suspended while waiting for other computations to complete. Let's see this in more detail.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.56.12