Created
January 27, 2011 14:24
-
-
Save Topher-the-Geek/798562 to your computer and use it in GitHub Desktop.
@cpsParam seems to thwart implicit conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.me.sundry | |
import org.specs.Specification | |
import org.specs.mock.Mockito | |
import scala.util.continuations._ | |
class ContinuationTests extends Specification with Mockito { | |
class Rendezvous[T] { | |
private trait State { | |
def get: T @suspendable | |
def set(v: T) | |
} | |
private class Waiting extends State { | |
private type K = (T => Unit) | |
private var q = List[K]() // TODO: Multithreading? | |
def get = shift ((k: K) => q ::= k) | |
def set(v: T) = { | |
q foreach (_(v)) | |
s = new Ready(v) | |
}} | |
private class Ready(v: T) extends State { | |
def get = v | |
def set(v: T) = throw new IllegalStateException("Rendezvous already set.") | |
} | |
private var s = new Waiting(): State | |
def v = s.get | |
def v_=(v: T) = s.set(v) | |
} | |
"Continuations" should { | |
"do what i want" in { | |
val r = new Rendezvous[Int] | |
var i = 0 | |
// Compiling this line yields: | |
// value must_== is not a member of Int @cpsParam[Unit,Unit] | |
reset { r.v must_== 42; i += 1 } | |
reset { r.v must_== 42; i += 1 } | |
r.v = 42 | |
i must_== 3 | |
reset { r.v must_== 42; i += 1 } | |
reset { r.v must_== 42; i += 1 } | |
i must_== 5 | |
}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment