如何在匹配模式中获取case类的匹配实例

时间:2013-12-29 13:29:13

标签: scala match case-class

请查看以下代码:

  def receive: Receive = {
    case Begin(msg, expectedReply, signalSuccessWith, replyTo, receiver, pause) => {
      receiver ! msg
      val cancelHandle = context.system.scheduler.schedule(pause, pause, context.self, Retry)
      context.become(runtime(Begin(msg, expectedReply, signalSuccessWith, replyTo, receiver, pause), cancelHandle))
    }
    case _ =>
  }

我可以看到,当我调用Begin时,我必须完全复制案例类context.become的值,即传递其全部参数。这可行,但看起来像我的代码重复。有没有办法以某种方式按原样获取匹配的值并传递它?

1 个答案:

答案 0 :(得分:5)

就像那样:

def receive: Receive = {
  case fullMsg @ Begin(msg, expectedReply, signalSuccessWith, replyTo, receiver, pause) => {
    receiver ! msg
    val cancelHandle = context.system.scheduler.schedule(pause, pause, context.self, Retry)
    context.become(runtime(fullMsg, cancelHandle))
  }
  case _ =>
}