Scala 意义ref@Ping在阿克卡接受
我在akka上浏览了一些代码示例,发现了一个我想确定其含义的特定示例:Scala 意义ref@Ping在阿克卡接受,scala,pattern-matching,akka,actor,Scala,Pattern Matching,Akka,Actor,我在akka上浏览了一些代码示例,发现了一个我想确定其含义的特定示例: def receive:receive={ 案例original@Ping(x) =>//做事 case=>//做事 } Ping是示例中用于消息的case类。 但是那原版的是什么意思呢?是发信人吗?如果是,这种方法是否比使用sender变量有任何优势 对不起,我不能给你链接,因为我再也找不到了 不确定这是Akka的东西还是我不知道的高级Scala模式匹配功能 找到答案的最简单方法是尝试: case class Ping(
def receive:receive={
案例original@Ping(x) =>//做事
case=>//做事
}
Ping
是示例中用于消息的case类。
但是那原版的是什么意思呢?是发信人吗?如果是,这种方法是否比使用sender
变量有任何优势
对不起,我不能给你链接,因为我再也找不到了
不确定这是Akka的东西还是我不知道的高级Scala模式匹配功能
找到答案的最简单方法是尝试:
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
相当于msg
,它是Ping(10)
。@
符号允许您将整个匹配对象分配给标识符。最简单的方法是尝试:
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
相当于msg
,它是Ping(10)
。@
符号用于将整个匹配对象分配给标识符。这是一种称为变量绑定的Scala功能。它将匹配的值绑定到变量。您可以在这里找到更多示例这是一种称为变量绑定的Scala功能
。它将匹配的值绑定到变量。你可以在这里找到更多的例子