Scala Language Continuations Library

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

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

Syntax

  • reset { ... } // Continuations extend up to the end of the enclosing reset block
  • shift { ... } // Create a continuation stating from after the call, passing it to the closure
  • A @cpsParam[B, C] // A computation that requires a function A => B to create a value of C
  • @cps[A] // Alias for @cpsParam[A, A]
  • @suspendable // Alias for @cpsParam[Unit, Unit]

Remarks

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.



Got any Scala Language Question?