我有以下类:
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.
}(_)
^
有没有简单的方法来做这样的事情?
你为什么要这样写呢?
后面有什么问题?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)
}