Some format issue when I'm trying to quote the code, so here is the picture :(
image description here
import akka.actor.{Actor, ActorSystem, Props}
case class Number(n: Int)
case class String(s: String)
class DoublingActor extends Actor {
def receive: Receive = {
case Number(n) => println(s"Result of doubling $n: ${n*2}")
case String(s) => println(s"Result of doubling $s: ${s}${s}")
}
}
object Double extends App {
val system = ActorSystem("DoublerSystme")
val doubler = system.actorOf(Props[DoublingActor], "doubler")
doubler ! Number(5)
doubler ! String("test")
}
The thing is, the actor works fine with number, but how can I add the matching function that returns the string twice?
You should avoid naming your case class as String
, especially in your case, with String
parameter type:
case class String(s: String)
It would confuse the compiler to expect a parameter type different from the actual String
type you want. Your app will work if you explicitly specify java.lang.String
as the parameter type:
case class String(s: java.lang.String)
In any case, I would not recommend naming a case class as String
.
What behavior are you seeing? Due to your issue being wrapped in an image, I can't test for sure, but I'd expect that you are getting a compile error with the descriptive message object String is not a case class, nor does it have an unapply/unapplySeq member
.
Because it doesn't have the unapply
member, you'll need to modify your statement to
case s: String => println(s"Result of doubling $s: ${s}:${s}")