scala actor消息定义

ben*_*zen 3 message scala actor

我是否需要为要在scala actor上检索的消息定义类?

我试图把它弄清楚我错在哪里

  def act() {  
    loop {  
      react {  
        case Meet => foundMeet = true ;   goHome  
        case Feromone(qty) if (foundMeet == true) => sender ! Feromone(qty+1); goHome  
   }}}
Run Code Online (Sandbox Code Playgroud)

Bri*_*Hsu 7

您可以将其视为正常模式匹配,如下所示.

match (expr)
{
   case a =>
   case b =>
}
Run Code Online (Sandbox Code Playgroud)

所以,是的,你应该首先定义它,使用没有参数的Message的对象和那些有参数的case类.(正如Silvio Bierman指出的那样,事实上,你可以使用任何可以模式匹配的东西,所以我稍微修改了这个例子)

以下是示例代码.

import scala.actors.Actor._
import scala.actors.Actor

object Meet
case class Feromone (qty: Int)

class Test extends Actor
{
    def act ()
    {
        loop {
            react {
                case Meet => println ("I got message Meet....")
                case Feromone (qty) => println ("I got message Feromone, qty is " + qty)
                case s: String => println ("I got a string..." + s)
                case i: Int => println ("I got an Int..." + i)
            }
        }
    }
}

val actor = new Test
actor.start

actor ! Meet
actor ! Feromone (10)
actor ! Feromone (20)
actor ! Meet
actor ! 123
actor ! "I'm a string"
Run Code Online (Sandbox Code Playgroud)