Delimited Continuations in Scala

January 08, 2019

sbt configuration

See the compiler plugin support section of the sbt documentation for the latest configuration information.

For sbt 1.x and Scala 2.12.x, use the following:

/***
autoCompilerPlugins := true
scalaVersion := "2.12.8"
addCompilerPlugin("org.scala-lang.plugins" % "scala-continuations-plugin_2.12.2" % "1.0.3")
libraryDependencies += "org.scala-lang.plugins" %% "scala-continuations-library" % "1.0.3"
scalacOptions += "-P:continuations:enable"
*/

Example

import scala.util.continuations.reset
import scala.util.continuations.shift
reset {
  val greeting: String =
    shift { k: (String => Unit) =>
      k("Hello, world!")
      k("¡Hola, mundo!")
      k("Howdy, pardner!")
    }
  println(greeting)
}

Demo

This file is literate Scala, and can be run using Codedown:

$ curl -s https://earldouglas.com/posts/funtinuations/compiling.md |
  codedown scala > script.scala
$ sbt -Dsbt.main.class=sbt.ScriptMain script.scala
Hello, world!
¡Hola, mundo!
Howdy, pardner!