Fix Future type issues

This commit is contained in:
Derek Williams 2011-06-13 20:25:31 -06:00
parent d917f999cf
commit cbdfd0fe23
3 changed files with 14 additions and 40 deletions

View file

@ -62,22 +62,8 @@ class FutureSpec extends JUnitSuite {
def shouldFutureCompose { def shouldFutureCompose {
val actor1 = actorOf[TestActor].start() val actor1 = actorOf[TestActor].start()
val actor2 = actorOf(new Actor { def receive = { case s: String self reply s.toUpperCase } }).start() val actor2 = actorOf(new Actor { def receive = { case s: String self reply s.toUpperCase } }).start()
val future1 = actor1 ? "Hello" mapTo manifest[String] flatMap ((s: String) actor2 ? s) val future1 = actor1 ? "Hello" flatMap { case s: String actor2 ? s }
val future2 = actor1 ? "Hello" mapTo manifest[String] flatMap (actor2 ? (_: String)) val future2 = actor1 ? "Hello" flatMap { case i: Int actor2 ? i }
val future3 = actor1 ? "Hello" mapTo manifest[Int] flatMap (actor2 ? (_: Int))
assert((future1.get: Any) === "WORLD")
assert((future2.get: Any) === "WORLD")
intercept[ClassCastException] { future3.get }
actor1.stop()
actor2.stop()
}
@Test
def shouldFutureComposePatternMatch {
val actor1 = actorOf[TestActor].start()
val actor2 = actorOf(new Actor { def receive = { case s: String self reply s.toUpperCase } }).start()
val future1 = actor1 ? "Hello" collect { case (s: String) s } flatMap (actor2 ? _)
val future2 = actor1 ? "Hello" collect { case (n: Int) n } flatMap (actor2 ? _)
assert((future1.get: Any) === "WORLD") assert((future1.get: Any) === "WORLD")
intercept[MatchError] { future2.get } intercept[MatchError] { future2.get }
actor1.stop() actor1.stop()
@ -124,15 +110,15 @@ class FutureSpec extends JUnitSuite {
}).start() }).start()
val future1 = for { val future1 = for {
Res(a: Int) actor.?(Req("Hello")).mapTo[Res[Int]] Res(a: Int) actor ? Req("Hello")
Res(b: String) actor.?(Req(a)).mapTo[Res[String]] Res(b: String) actor ? Req(a)
Res(c: String) actor.?(Req(7)).mapTo[Res[String]] Res(c: String) actor ? Req(7)
} yield b + "-" + c } yield b + "-" + c
val future2 = for { val future2 = for {
Res(a: Int) actor.?(Req("Hello")) Res(a: Int) actor ? Req("Hello")
Res(b: Int) actor.?(Req(a)).mapTo[Res[Int]] Res(b: Int) actor ? Req(a)
Res(c: Int) actor.?(Req(7)).mapTo[Res[Int]] Res(c: Int) actor ? Req(7)
} yield b + "-" + c } yield b + "-" + c
assert(future1.get === "10-14") assert(future1.get === "10-14")

View file

@ -575,7 +575,7 @@ sealed trait Future[+T] {
} }
} }
/*final def withFilter(p: T ⇒ Boolean) = new FutureWithFilter[T](this, p) final def withFilter(p: T Boolean) = new FutureWithFilter[T](this, p)
final class FutureWithFilter[+A](self: Future[A], p: A Boolean) { final class FutureWithFilter[+A](self: Future[A], p: A Boolean) {
def foreach(f: A Unit): Unit = self filter p foreach f def foreach(f: A Unit): Unit = self filter p foreach f
@ -584,8 +584,7 @@ sealed trait Future[+T] {
def withFilter(q: A Boolean): FutureWithFilter[A] = new FutureWithFilter[A](self, x p(x) && q(x)) def withFilter(q: A Boolean): FutureWithFilter[A] = new FutureWithFilter[A](self, x p(x) && q(x))
} }
final def filter(p: T Boolean): Future[T] = { */ final def filter(p: T Boolean): Future[T] = {
final def filter(p: Any Boolean): Future[T] = {
val f = new DefaultPromise[T](timeoutInNanos, NANOS) val f = new DefaultPromise[T](timeoutInNanos, NANOS)
onComplete { ft onComplete { ft
val optv = ft.value val optv = ft.value
@ -621,17 +620,6 @@ sealed trait Future[+T] {
} else None } else None
} }
/* Java API */
final def onComplete[A >: T](proc: Procedure[Future[A]]): this.type = onComplete(proc(_))
final def map[A >: T, B](f: JFunc[A, B]): Future[B] = map(f(_))
final def flatMap[A >: T, B](f: JFunc[A, Future[B]]): Future[B] = flatMap(f(_))
final def foreach[A >: T](proc: Procedure[A]): Unit = foreach(proc(_))
final def filter(p: JFunc[Any, Boolean]): Future[Any] = filter(p(_))
} }
object Promise { object Promise {

View file

@ -3,28 +3,28 @@ package akka.japi
/** /**
* A Function interface. Used to create first-class-functions is Java (sort of). * A Function interface. Used to create first-class-functions is Java (sort of).
*/ */
trait Function[T, R] { abstract class Function[-T, +R] extends (T) => R {
def apply(param: T): R def apply(param: T): R
} }
/** /**
* A Function interface. Used to create 2-arg first-class-functions is Java (sort of). * A Function interface. Used to create 2-arg first-class-functions is Java (sort of).
*/ */
trait Function2[T1, T2, R] { abstract class Function2[-T1, -T2, +R] extends (T1, T2) => R {
def apply(arg1: T1, arg2: T2): R def apply(arg1: T1, arg2: T2): R
} }
/** /**
* A Procedure is like a Function, but it doesn't produce a return value * A Procedure is like a Function, but it doesn't produce a return value
*/ */
trait Procedure[T] { abstract class Procedure[-T] extends (T) => Unit {
def apply(param: T): Unit def apply(param: T): Unit
} }
/** /**
* A Procedure is like a Function, but it doesn't produce a return value * A Procedure is like a Function, but it doesn't produce a return value
*/ */
trait Procedure2[T1, T2] { abstract class Procedure2[-T1, -T2] extends (T1, T2) => Unit {
def apply(param: T1, param2: T2): Unit def apply(param: T1, param2: T2): Unit
} }