Thursday, January 28, 2010

Scala 2.8.0 beta 1 released

The first Scala 2.8 beta has been released. It has a lot of new features. I'm particularly looking forward to using the improved collections, type specialization, default arguments, and of course continuations!

The continuations plugin hasn't been merged into the default distribution yet. If you'd like to use continuations with the beta then you'll need to build it yourself.

Building the plugin

The process for building the plugin is pretty much as I described in my original post. One of the JAR files has been moved out of the standard Scala distribution, so you'll need to download it manually (using the second wget below).

First download the Scala 2.8 beta.

$ wget http://www.scala-lang.org/downloads/distrib/files/scala-2.8.0.Beta1-prerelease.tgz
$ tar xzf scala-2.8.0.Beta1-prerelease.tgz
$ wget http://scala-tools.org/repo-releases/org/scala-lang/scala-partest/2.8.0.Beta1/scala-partest-2.8.0.Beta1.jar -O scala-2.8.0.Beta1-prerelease/lib/scala-partest.jar
$ export SCALA_HOME=`pwd`/scala-2.8.0.Beta1-prerelease

Next get the source for the continuations plugin.

$ svn co http://lampsvn.epfl.ch/svn-repos/scala/compiler-plugins/continuations/trunk continuations

Then build it and run the tests.

$ cd continuations
$ ANT_OPTS=-Xmx512m ant test

Finally set an environment variable to point to the continuations code.

$ export CONT_HOME=`pwd`

Compiling and running a program that uses continuations

Here's a short example program that uses continuations.

$ cat > example.scala
import scala.continuations.ControlContext._

object Example {
  def main(args: Array[String]) {
    reset {
      println("first")
      shift { k: (Unit => Unit) => k(k(())) }
      println("last")
    }
  }
}

To compile it you'll need to load the continuations plugin into the compiler.

$ $SCALA_HOME/bin/scalac -Xplugin:$CONT_HOME/build/pack/selectivecps-plugin.jar -classpath $CONT_HOME/build/build.library example.scala

To run it just make sure you include the continuations runtime library.

$ $SCALA_HOME/bin/scala -classpath $CONT_HOME/build/build.library:. Example
first
last
last

And that's it.

7 comments:

Yang said...

Hi, I made this post on StackOverflow asking if there's any way to implement C#'s yield-return using delimited continuations:

http://stackoverflow.com/questions/2201882/implementing-yield-yield-return-using-scala-continuations

Do you know if this is possible?

Rich Dougherty said...

Yes it's possible. I've posted an answer that shows one way to do it.

kaimeder said...

I cant get it compiled :(

[scalac] /home/kai/dev/scala-2.8.0.Beta1-prerelease/continuations/src/plugin/scala/continuations/CPSAnnotationChecker.scala:153: error: value BYVALmode is not a member of object CPSAnnotationChecker.this.global.analyzer
[scalac] if (annots1.isEmpty && !annots2.isEmpty && ((mode & global.analyzer.BYVALmode) == 0)) {
[scalac]

Rich Dougherty said...

Hi Kaimeder

There have been some updates to the continuations code recently. Maybe a recent change is causing the problem? My code was built with revision 20706. You could try updating your code and rebuilding. The following should work:

svn up -r 20706

- Rich

KHM said...

Thanks a ton Rich, works!

BJacobs said...

Could it be that your trampoline is not completely lazy? Specifically, it seems that the thunk will be executed up to the first yield at the time the trampoline is created. So the first value is computer eagerly.

Rich Dougherty said...

That's a good point. The first item is in fact evaluated immediately. This is a side-effect of using a Stream to help create the Iterator. A better way might be to write a custom Iterator.