0

我有以下课程:

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.
    }(_)
     ^

有没有捷径可以做这样的事情?

4

1 回答 1

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)
}
于 2016-08-23T17:29:52.253 回答