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 .

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 { println ( "A" ) shift { k1 : ( Unit => Unit ) => println ( "B" ) k1 () println ( "C" ) } println ( "D" ) shift { k2 : ( Unit => Unit ) => println ( "E" ) k2 () println ( "F" ) } println ( "G" ) }

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

The output would be

A B D E G F C

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.

I hope you enjoyed the blog. ! Keep reading 🙂

References: