diff options
author | Philipp Haller <hallerp@gmail.com> | 2008-04-23 14:44:00 +0000 |
---|---|---|
committer | Philipp Haller <hallerp@gmail.com> | 2008-04-23 14:44:00 +0000 |
commit | 142bf7242ec3294afeb1a1ba26ab15183934be3e (patch) | |
tree | 889b5f4394d02fff7dfc3352a6027c14415da06a /src/actors | |
parent | 90a862787e51dc5c5d490988ee3dbe5c7556a3bc (diff) | |
download | scala-142bf7242ec3294afeb1a1ba26ab15183934be3e.tar.gz scala-142bf7242ec3294afeb1a1ba26ab15183934be3e.tar.bz2 scala-142bf7242ec3294afeb1a1ba26ab15183934be3e.zip |
Added support for defining actors using s (usin...
Added support for defining actors using s (using the factory method).
s are now also s. Actors have an additional method which makes methods
like usable inside s.
Diffstat (limited to 'src/actors')
-rw-r--r-- | src/actors/scala/actors/Actor.scala | 78 | ||||
-rw-r--r-- | src/actors/scala/actors/Future.scala | 13 |
2 files changed, 80 insertions, 11 deletions
diff --git a/src/actors/scala/actors/Actor.scala b/src/actors/scala/actors/Actor.scala index 2def07b6c8..84efe9c27f 100644 --- a/src/actors/scala/actors/Actor.scala +++ b/src/actors/scala/actors/Actor.scala @@ -70,8 +70,11 @@ object Actor { } /** - * <p>This function is used for the definition of actors.</p> - * <p>The following example demonstrates its usage:</p><pre> + * <p>This is a factory method for creating actors.</p> + * + * <p>The following example demonstrates its usage:</p> + * + * <pre> * import scala.actors.Actor._ * ... * val a = actor { @@ -91,6 +94,39 @@ object Actor { } /** + * <p> + * This is a factory method for creating actors whose + * body is defined using a <code>Responder</code>. + * </p> + * + * <p>The following example demonstrates its usage:</p> + * + * <pre> + * import scala.actors.Actor._ + * import Responder.exec + * ... + * val a = reactor { + * for { + * res <- b !! MyRequest; + * if exec(println("result: "+res)) + * } yield {} + * } + * </pre> + * + * @param body the <code>Responder</code> to be executed by the newly created actor + * @return the newly created actor. Note that it is automatically started. + */ + def reactor(body: => Responder[Unit]): Actor = { + val a = new Actor { + def act() { + Responder.run(body) + } + } + a.start() + a + } + + /** * Receives the next message from the mailbox of the current actor * <code>self</code>. */ @@ -186,6 +222,28 @@ object Actor { */ def mailboxSize: Int = self.mailboxSize + /** + * <p> + * Converts a synchronous event-based operation into + * an asynchronous <code>Responder</code>. + * </p> + * + * <p>The following example demonstrates its usage:</p> + * + * <pre> + * val adder = reactor { + * for { + * _ <- async(react) { case Add(a, b) => reply(a+b) } + * } yield {} + * } + * </pre> + */ + def async(fun: PartialFunction[Any, Unit] => Nothing): + PartialFunction[Any, Unit] => Responder[Any] = + (caseBlock: PartialFunction[Any, Unit]) => new Responder[Any] { + def respond(k: Any => Unit) = fun(caseBlock andThen k) + } + private[actors] trait Body[a] { def andThen[b](other: => b): Unit } @@ -292,7 +350,7 @@ object Actor { * </li> * </ul> * - * @version 0.9.14 + * @version 0.9.15 * @author Philipp Haller */ @serializable @@ -556,6 +614,11 @@ trait Actor extends OutputChannel[Any] { else ch.receive { case any => value = Some(any); any } + def respond(k: Any => Unit): Unit = + if (isSet) k(value.get) + else ch.react { + case any => value = Some(any); k(any) + } def isSet = value match { case None => ch.receiveWithin(0) { case TIMEOUT => false @@ -578,10 +641,15 @@ trait Actor extends OutputChannel[Any] { send(msg, ftch) new Future[A](ftch) { def apply() = - if (isSet) value.get + if (isSet) value.get.asInstanceOf[A] else ch.receive { - case any => value = Some(f(any)); value.get + case any => value = Some(f(any)); value.get.asInstanceOf[A] } + def respond(k: A => Unit): Unit = + if (isSet) k(value.get.asInstanceOf[A]) + else ch.react { + case any => value = Some(f(any)); k(value.get.asInstanceOf[A]) + } def isSet = value match { case None => ch.receiveWithin(0) { case TIMEOUT => false diff --git a/src/actors/scala/actors/Future.scala b/src/actors/scala/actors/Future.scala index 14c838bf0f..c40ae1e4bb 100644 --- a/src/actors/scala/actors/Future.scala +++ b/src/actors/scala/actors/Future.scala @@ -12,9 +12,10 @@ package scala.actors /** * <p> - * A Future is a function of arity 0 that returns a value of type Any. - * Applying a future blocks the current actor until its value - * is available. + * A <code>Future[T]</code> is a function of arity 0 that + * returns a value of type <code>T</code>. + * Applying a future blocks the current actor (<code>self</code>) + * until the future's value is available. * </p> * <p> * A future can be queried to find out whether its value @@ -22,10 +23,10 @@ package scala.actors * </p> * * @author Philipp Haller - * @version 0.9.8 + * @version 0.9.15 */ -abstract class Future[T](val ch: InputChannel[Any]) extends Function0[T] { - protected var value: Option[T] = None +abstract class Future[+T](val ch: InputChannel[Any]) extends Responder[T] with Function0[T] { + protected var value: Option[Any] = None def isSet: Boolean } |