Speculative idea: Continuations


This is a speculative idea for discussion, I realize that it would entail a bunch of work.

A few years back I came up with an idea I called “Swarm”, see here for a detailed description: http://code.google.com/p/swarm-dpl/

Briefly, the idea is to use Scala’s portable continuations (via the delimited continuations compiler plugin that came with Scala 2.8) to build a system that could seamlessly distribute computation across multiple computers in an efficient way.  It’s a little like Google’s “Map Reduce” concept, except not limited to algorithms that can be broken down into Map and Reduce operations.

One application for this concept that I didn’t explore in too much depth was the idea of using portable continuations to transfer program flow between a web browser and a web server.  The potential would be to make communication between browser and server almost completely transparent to the programmer.  Some code would run on the server (that which needs to interact with a database, or that which must be secure), and some would run in the client.  The system would automatically determine which is which.

Of course, a major obsticle to doing this with Scala was the fact that Scala couldn’t compile to JavaScript (at least not without some kludgy use of GWT), however Kotlin does.

So the main missing component from the language would be some kind of continuations support - probably similar to the delimited continuations plugin in Scala, which operates through a code transformation.  How fesable would this be?


We will not invest into explicit continuations in any near future. Some more disciplined mechanisms for handling asynchronous computations may be implemented in a year or so.


Out of curiousity, can you elaborate on "more disciplined"?


Explicit continuations is a kind of superpower: you can do so much with them that it's hard to control. An example of more disciplined approach: async in C# or F#


Right, but if I'm reading it correctly, async is just language support for Futures.  Continuations are vastly more powerful.  Of course, as Uncle Ben said, "with great power comes great responsibility", but I don't think that's an argument against a language feature.

Continuations may only be used by a minority of programmers, but the things those programmers build with continatuions might be very widely usable.


It is a huge argument agains a language feature. Both the "great responsibility" and that only a minority will use it.


I don't know whether you have seen Coherence from Oracle. It also has a feature to move the computation to the data. This is happening on application-level only, without support by the language or vm or whatever. So things cannot be done in a generic way. The application developer has to help the system with saving the context to move it somewhere else. Nevertheless, Oracle payed a lot of money to acquire Tangosol back then to get hold of Coherence. If you look at the license fees that stuff does not come cheap. A lot of people are willing to pay a lot of money for being able to move the computation to the data. And they don't seem to care that much whether this is happening at the application-level as some compromise or generically on a lower level. Eventually, it might be worth some consideration whether weakening the requirements a bit is an acceptable compromise.

Other than that there was the Plurix project at the university of Ulm (in Germany) where they extended the jvm to be able to suspend some Java thread and then evict it to some machine with less load. But it seems that the project is meanwhile dead. But you might find some documents about it on Citeseer. Amoeba, Sprite and Mosix are also interesting and might still exist. But the eviction with these systems is happening on the OS level. So this might not be what you are looking for.

– Oliver


That is very interesting, thank you.


A minority may use it directly, but a majority might use it indirectly by using libraries that are built with it.

As for great power, it’s not like goto where people would be tempted to use it in ways that would lead to bad code, it’s not a shortcut, it’s a control structure that let’s you do things that can’t be done in another way.


As a matter of coincidence: http://en.wikipedia.org/wiki/Goto#Continuations


What is the current state of explicit continuations in the context of Kotlin coroutines? I saw the design proposal makes extensive use of continuations internally and was curious if your thoughts on offering explicit continuations have changed in the meantime, given their usefulness? Are there any plans to provide a shift-reset operator similar to Scala’s delimited continuations, or do you know what the analogous coroutine pattern might be? Thanks! cc: @elizarov