我有以下课程:
class MessageProcessorActor(private val destination : ActorRef) extends Actor{
override def receive: Receive = {
case _ =>
destination ! {
case MyActor.TracableMessage(msg) => MyActor.TracableMessage("Processed + " + msg)
case MyActor.Message(msg) => MyActor.Message("Processed + " + msg)
case _ => "Processed + " + _
}(_)
}
}
object MyActor{
case class TracableMessage(private val msg: String) extends Message(msg)
case class Message(private val msg: String)
}
Recieve
定义为
type Receive = PartialFunction[Any, Unit]
在这里,我试图避免声明val
持有PartialFunction[Any, Any]
并以此方式调用它。但是编译器拒绝这个代码。它打印:
Error:(12, 6) ';' expected but '(' found.
}(_)
^
这样做有什么简短的方法吗?
答案 0 :(得分:1)
你为什么要这样写呢?以下
有什么问题class MessageProcessorActor(private val destination : ActorRef) extends Actor{
override def receive: Receive = {
case MyActor.TracableMessage(msg) => {
destination ! MyActor.TracableMessage("Processed + " + msg)
}
case MyActor.Message(msg) => {
destination ! MyActor.Message("Processed + " + msg)
}
case _ => {
destination ! "Processed + " + _
}
}
}
object MyActor{
case class TracableMessage(private val msg: String) extends Message(msg)
case class Message(private val msg: String)
}