akka receive中ref@Ping的含义

Meaning of ref@Ping in akka receive

我在 akka 上浏览了一些代码示例,我发现了一个我想确定其含义的特定示例:

def receive: Receive = { case original@Ping(x) => // do stuff case _ => //do stuff }

Ping 是示例中用于消息的大小写 class。 但是 original@ 是什么意思?是消息发送者吗?如果是这样,这种方法比使用 sender 变量有什么优势吗?

抱歉,我不能给你 link 因为我找不到了...

Not sure if this Akka thing or just a advanced Scala pattern matching feature that I wasn't aware..

找出答案的最简单方法是尝试一下:

case class Ping(x: Int)

scala> val msg = Ping(10)
msg: Ping = Ping(10)

scala> msg match {
     |    case original @ Ping(x) => {
     |        println("Original: " + original)
     |        println("x: " + x)
     |    }
     |    case _ => println("no match")
     | }
Original: Ping(10) // Printed the entire msg that was matched
x: 10              // Printed just the value x that was matched

所以 original 等同于 msgPing(10)@ 符号可让您将整个匹配对象分配给一个标识符。

这是一个名为 variable binding 的 Scala 特性。它将匹配的值绑定到变量。您可以在此处找到更多示例 Scala @ operator