Returning Value from Actor

Kevin Meredith

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
adelbertc

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Returning a value from a Method

분류에서Dev

Returning a value from a Thread?

분류에서Dev

Returning a value from a recursive function

분류에서Dev

Returning a value from a function with Alamofire and SwiftyJson

분류에서Dev

Windows Form Not Returning Correct Value from Public Method

분류에서Dev

Better Practice: Printing from void method OR returning value from method and printing from method caller

분류에서Dev

Is it required to explicitly stop an Akka actor from supervisor?

분류에서Dev

PHP function not returning value

분류에서Dev

AngularJS : service not returning value

분류에서Dev

ArrayList returning null value

분류에서Dev

strlen returning wrong value

분류에서Dev

Why is diff returning an incorrect value?

분류에서Dev

numpy not returning the correct median value

분류에서Dev

Conditional SUM not returning any value

분류에서Dev

Functions keeps returning the same value

분류에서Dev

Function not returning the Value of its Key

분류에서Dev

Is checking if a variable is equal to a value and then returning that value redundant?

분류에서Dev

Returning a pointer to a structure from function

분류에서Dev

Returning values from an Object in Javascript

분류에서Dev

Return statement in JavaScript is not returning the right value

분류에서Dev

$(li).width() returning value which includes padding?

분류에서Dev

function returning wrong value while executing

분류에서Dev

Jquery UI Mobile returning empty value for form

분류에서Dev

ListView using findViewById is returning null value

분류에서Dev

Retrieve URL Value but keeps returning (NULL)

분류에서Dev

Why is AES function returning different value?

분류에서Dev

array_search returning wrong value

분류에서Dev

Mysql_num_rows not returning any value

분류에서Dev

Returning the wrong value in basic Java program

Related 관련 기사

뜨겁다태그

보관