2015-05-23 37 views
0

我是衝浪的阿卡一些代碼示例,我發現,我想是肯定的意思一個具體的例子:意義阿卡收到

def receive: Receive = { case [email protected](x) => // do stuff case _ => //do stuff }

Ping是一個案例類用於示例中的消息。 但是[email protected]的含義是什麼?它是消息發送者嗎?如果是這樣,這種方法在使用sender變量時有什麼優勢嗎?

很抱歉,但我不能給你的鏈接,因爲我無法找到它了...

不知道這件事阿卡或只是一個高級的Scala模式匹配功能,我沒」 •解..

回答

3

這是一個名爲variable binding的Scala功能。它將匹配的值綁定到變量上。你可以在這裏找到更多的例子Scala @ operator

3

最簡單的方法,找出是嘗試一下:

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)@符號可讓您將整個匹配對象分配給標識符。