Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does pattern matching work in Akka.Actor receive method?

Tags:

scala

akka

I am new to Scala programming and having some trouble understanding how actors works and how to use them correctly.

Looking at the source code of an Akka actor, the following is exposed:

trait Actor {
  def receive: Actor.Receive // Actor.Receive = PartialFunction[Any, Unit]
}

My first impression of this is that Actor is a trait that exposes one abstract method, receive which takes no arguments and then returns a partial function. First question, is this the correct interpretation of the API?

Next, I looked at the documentation for how to implement actors. The examples look something like this:

class HelloActor extends Actor {
  def receive = {
    case "hello" => println("hello back at you")
    case _       => println("huh?")
  }
}

Clearly there is some pattern matching going on here, but I'm having trouble understanding how this works. For instance, let's say I wanted to invoke the receive method directly without using something like send, how would I do it?

like image 796
Max Avatar asked Dec 07 '25 19:12

Max


1 Answers

as others already answered, you never should directly call methods on Actors. But your question seems to be more about "how does PartialFunction work in Scala?", right?

In Scala the PartialFunction[In, Out] has a bit of compiler generated code for methods like isDefinedAt, so you can ask a partial function if it's defined for a certain argument:

scala> val fun: PartialFunction[Any, String] = {
     | case 42 => "yes"
     | }
fun: PartialFunction[Any,String] = <function1> 

scala> fun.isDefinedAt(12)
res0: Boolean = false

scala> fun.isDefinedAt(42)
res1: Boolean = true

scala> fun(42)
res2: String = yes

scala> fun(12)
scala.MatchError: 12 (of class java.lang.Integer)

```

We use the isDefinedAt method before we apply a message to a receive function, and if it's not able to handle the message we pass it into unhandled(msg), which usually just logs the message to dead letters so you can figure out that your Actor didn't handle a message.

like image 194
Konrad 'ktoso' Malawski Avatar answered Dec 10 '25 09:12

Konrad 'ktoso' Malawski



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!