I’m trying to reproduce this example.
Everything compiles, but when I run it, all my results look like this:
scala> NameResolver ! ("www.scala-lang.org", self)
scala> self.receiveWithin(0) { case x => x }
res0: Any = TIMEOUT // and not Some(www.scala-lang.org/128.178.154.102)
scala> NameResolver ! ("wwwwww.scala-lang.org", self)
scala> self.receiveWithin(0) { case x => x }
res1: Any = TIMEOUT // and not None
Here is my example:
import scala.actors._
import scala.actors.Actor._
case class Plus(x: Int, y: Int)
val concurrentCalculator = actor {
while(true)
receive {
case Plus(x, y) => println(x + y)
case (Plus(x, y), caller: Actor) => caller ! (x + y)
}
}
scala> concurrentCalculator ! Plus(2,3)
5
scala> concurrentCalculator ! (Plus(2,3), self)
scala> self.receiveWithin(1000) { case x => x }
res0: Any = TIMEOUT // WTF?
So why am I getting a TIMEOUT instead of a valid result?
You can check that
selfreturns different values on different calls not within a code block, due to the way Scala REPL works (every expression to be evaluated is compiled into a separate class):In one block of code it will work even if it isn’t explicitly declared as an actor: