Time Travel in Scala: CPS in Scala (scala’s continuation).

Table of contents
Reading Time: 3 minutes

Sometime before I was just roaming around in the landOfLisp  and I came across different spaceships of different guilds of Functional Programming that are going to save the earth from the unholy creatures: Bugs ! That degrade our software quality.

I was trying to map everything to Scala as it can also be written in a functional way. Then I came across a guild name Continuation. I was quite fascinated with this because it allowed us to travel time in our code.

So basically , continuations is a way to put “time travel” into our code.

In a more technical way ,

A continuation is an abstract representation of the control state of a computer program. So what it actually means is that it is a data structure that represents the computational process at a given point in the process’s execution; the created data structure can be accessed by the programming language, instead of being hidden in the runtime environment.

To explain it further we can have one of the most classic example,

Say you’re in the kitchen in front of the refrigerator, thinking about a sandwich. You take a continuation right there and stick it in your pocket. Then you get some turkey and bread out of the refrigerator and make yourself a sandwich, which is now sitting on the counter. You invoke the continuation in your pocket, and you find yourself standing in front of the refrigerator again, thinking about a sandwich. But fortunately, there’s a sandwich on the counter, and all the materials used to make it are gone. So you eat it. 🙂

In this description, the sandwich is part of the program data (e.g., an object on the heap), and rather than calling a “make sandwich” routine and then returning, the person called a “make sandwich with current continuation” routine, which creates the sandwich and then continues where execution left off.

Wasn’t that amazing !

Continuations can solve many difficult high-level problems, like programming a web server that supports multiple pages, accessed by the use of the forward and back buttons and by following links.

Here is some history taken from land of Lisp, if you want to check it out .

Screenshot from 2016-04-30 14-37-27

So how does Scala supports it ?

Scala supports Continuation from its version 2.8 inside the scala.util.continuations

Dependency can be added inside the build.sbt like this

libraryDependencies += “org.scala-lang.plugins” % “scala-continuations-plugin_2.11.1” % “1.0.2”

You can also look for the newest version here.

Now here is a quick example for defining how they work.

reset {
    shift { k1: (Unit=>Unit) =>
    shift { k2: (Unit=>Unit) =>

Now what do you think ? What should be the output. ? Take a guess!

The output would be


Hence from this example you can see that how the control is passing to and fro.

For more insight you can refer to Jim-Macbeth’s blog

Scala’s continuations are most commonly used in scala’s async library  which is a way for implementing the asynchronous block of code just like Future. Currently scala is supporting the continuations but as they have stated in the news letter

We are looking for maintainers to take over the following modules: scala-swing, scala-continuations. 2.12 will not include them if no new maintainer is found. We will likely keep maintaining the other modules (scala-xml, scala-parser-combinators), but help is still greatly appreciated.

So they are looking for a maintainer and i hope they find it.!

Here are some more links related to this topic.

  1. A Taste of 2.8: Continuations

  2. Jim-Macbeth’s blog
  3. What are scala’s continuation and why use them?

I hope you enjoyed the blog. ! Keep reading 🙂


  1. Land Of Lisp
  2. A Taste of 2.8: Continuations

  3. Jim-Macbeth’s blog
  4. What are scala’s continuation and why use them?
  5. Wikipedia

2 thoughts on “Time Travel in Scala: CPS in Scala (scala’s continuation).3 min read

Comments are closed.