Scala Spores, safe mobile closures: SIP-21
The Spores project is built and tested using sbt. It has two modules:
spores-core
and spores-pickling
. The spores-core
module contains the
core type definitions and the spore
macro. The spores-pickling
module
integrates Spores with scala/pickling
by providing picklers for Spores.
To build the core Spores module:
> project spores-core
> compile
To run the test suite:
> test
The spores-core
and spores-pickling
modules for Scala 2.11 are available
on Maven Central and Sonatype. You can use Spores in your sbt project by
simply adding the following dependency to your build file:
libraryDependencies += "org.scala-lang.modules" %% "spores-core" % "0.1.3"
To enable integration with Pickling, add the following dependency:
libraryDependencies += "org.scala-lang.modules" %% "spores-pickling" % "0.1.3"
Or you can just directly download the jar files (spores-core, spores-pickling).
Many users expressed concern that spores would be both unusable with for- expression syntax, and would be incompatible (without a lot of boilerplate) with normal closures or higher-order functions which take normal functions as arguments.
To remedy the incompatibility with for-expressions, we propose a new capture
syntax. Here is an exemplary use in the context of a hypothetical
DCollection
type:
def lookup(i: Int): DCollection[Int] = ...
val indices: DCollection[Int] = ...
for { i <- indices
j <- lookup(i)
} yield j + capture(i)
trait DCollection[A] {
def map[B](sp: Spore[A, B]): DCollection[B]
def flatMap[B](sp: Spore[A, DCollection[B]): DCollection[B]
}
A stable path is an expression which only contains selections and identifiers (no applications, for example), and for which each selected entity is stable. In this context, stable means that the entity or object in question is introduced by object definitions or by value definitions of non-volatile types.
Adapted from the Scala Language specification (section 3.1), a path is defined to be one of the following:
C.this
, whereC
references a class. The paththis
is taken as a shorthand forC.this
whereC
is the name of the class directly enclosing the reference.x
wherex
is a package.p.x
wherep
is a path andx
is a stable member ofp
. Stable members are packages or members introduced by object definitions or by value definitions of non-volatile types. (Section 3.6 of the SLS.)C.super.x
orC.super[M].x
whereC
references a class andx
references a stable member of the super class or designated parent classM
ofC
. The prefixsuper
is taken as a shorthand forC.super
whereC
is the name of the class directly enclosing the reference.
A path refers to an object, that is, it ends with an identifier.
- Need to make it more convenient to create a nullary spore
- Should objects be allowed in paths? The reason is that they are initialized lazily, so if we don't allow lazy vals, then allowing objects (which could end up being initialized only when the spore is applied) doesn't make a lot of sense.