Closed scabug closed 13 years ago
Imported From: https://issues.scala-lang.org/browse/SI-781?orig=1 Reporter: @richdougherty Attachments:
@phaller said: Examples of Responder-based actors.
@phaller said:
Adding support for writing actors using Responder
s has been discussed for a long time now. (See for example http://article.gmane.org/gmane.comp.lang.scala.user/1350/.)
In r14801 I committed initial support for that. It generalizes the proposals discussed in the mailing list thread referenced above by introducing the async
method (not sure whether the name is ideal). This method can be used to make methods like react
usable inside Responder
s. I attach the source code for some examples that demonstrates the usage.
For convenience there is also a reactor
factory method for creating Responder
-based actors. It takes a Responder
as argument.
An idea that I like is to make Future
s into Responder
s. Note that this makes the !%
method that you propose obsolete: your above example can be written as follows:
val a = reactor {
for {
x <- adder !! Add(1, 2);
y <- adder !! Add(3, 4);
z <- adder !! Add(x.asInstanceOf[Int], y.asInstanceOf[Int]);
if exec(println("Sum is: " + z))
} yield {}
}
I invite people to try out this extension, and give feedback on the mailing lists.
I close this ticket now.
@richdougherty said: Looks good, thanks.
@odersky said: Milestone next_bugfix deleted
Using actors with asynchronous operations can currently result in messy code when compared to using synchronous operations. This is because it is often necessary to nest the continuations for each reaction, even when the logic is basically linear.
eg
vs
However, using Responders and for comprehensions can make a continuation-passing style much clearer. The following patch adds support for Responders to the Actor class.
%
as an asynchronous alternative to?
.!%
as an asynchronous alternative to!?
.eg