For the following Actor that checks divisibility by 3:
class CheckActor(actor: Actor) extends Actor {
println("created actor")
var sum = 0
def act = loop {
react {
case x:Int if (x % 3 == 0) => sum+=1
...
case "calcSum" => sum
case _ =>
}
}
}
When I try to get the value of sum
from the CheckActor instance...
actor1 ! "calcSum" // actor1 is of type "CheckActor"
I see this error on the type mismatch of sum1.
$>scalac Count.scala
Count.scala:38: error: type mismatch;
found : Unit
required: Int
val sum1: Int = actor1 ! "calcSum"
^
one error found
The action of sending a message (actor ! foo
) returns Unit
because there is nothing to return, you fire and forget (asynchronous). If you want/expect some sort of response back, use ask
or ?
instead - this will return to you a Future[Any]
that you can use.
On a related note, it looks like you're using Scala Actors, I suggest you look at Akka (http://akka.io/) if you want to look into actors - as of Scala 2.10.0 Scala Actors are deprecated and replaced by Akka.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다