Continuation passing style is a form of control flow that involves passing to functions the rest of the computation as a "continuation" argument. The function in question later invokes that continuation to continue program execution. One way to think of a continuation is as a closure. The Scala continuations library brings delimited continuations in the form of the primitives shift
/reset
to the language.
continuations library: https://github.com/scala/scala-continuations
shift
and reset
are primitive control flow structures, like Int.+
is a primitive operation and Long
is a primitive type. They are more primitive than either in that delimited continuations can actually be used to construct almost all control flow structures. They are not very useful "out-of-the-box", but they truly shine when they are used in libraries to create rich APIs.
Continuations and monads are also closely linked. Continuations can be made into the continuation monad, and monads are continuations because their flatMap
operation takes a continuation as parameter.