Akka and affairs

I am learning Akka for Scala, and also reading the / case template statements in Scala.

In Akka, I can write an actor as follows:

class MyActor extends Actor {
    def receive: Receive = {
        case msg: MyMsgClass => sender ! "message received!"
    }
}

Questions:

  • Is this an example of matching a Scala pattern? If so, why is there no corresponding keyword match?

  • caseIs there an msgidentifier in the line and is it required? What happens if I omit it and just use the class name (and, presumably, if I do not specify an identifier, I cannot use this variable)?

+4
source share
2 answers

This is a case of matching patterns in combination with a partial function. In short, partial function

{
    case msg: MyMsgClass
}

MyMsgClass. " " . PartialFunction, , MyMsgClass.

:

{
    case MyMsgClass(value) => sender ! value
}

value. , :

{
    case m @ MyMsgClass(AnotherClass(_), "this must be this string", a) => sender ! doSomething(m, a)
}

MyMsgClass, AnotherClass ( , , _), "this must be this string" a. m @ , .

: http://blog.bruchez.name/2011/10/scala-partial-functions-without-phd.html

+6

Scaladocs for Actor , Receive : type Receive = PartialFunction [Any, Unit], , .

+1

All Articles