Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala actor message definition

Do i need to define class for message i want to retrieve on a scala actor?

i trying to get this up where am i wrong

  def act() {  
    loop {  
      react {  
        case Meet => foundMeet = true ;   goHome  
        case Feromone(qty) if (foundMeet == true) => sender ! Feromone(qty+1); goHome  
   }}}
like image 774
benzen Avatar asked Apr 13 '26 09:04

benzen


1 Answers

You can think it as a normal pattern matching just like the following.

match (expr)
{
   case a =>
   case b =>
}

So, yes, you should define it first, use object for Message without parameters and case class for those has parameters. (As Silvio Bierman pointed out, in fact, you could use anything that could be pattern-matched, so I modified this example a little)

The following is the sample code.

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"
like image 51
Brian Hsu Avatar answered Apr 15 '26 03:04

Brian Hsu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!