我试图让Scala的演员(阿卡)一握,但我只是碰到“的情况下”使用的一些奇怪的是一点我不明白,就来了:Scala的情况下,语法的理解
import akka.actor.{ ActorRef, ActorSystem, Props, Actor, Inbox }
import scala.concurrent.duration._
case object Greet
case class WhoToGreet(who: String)
case class Greeting(message: String)
class Greeter extends Actor {
var greeting = ""
def receive = {
case WhoToGreet(who) => greeting = s"hello, $who"
case Greet => sender ! Greeting(greeting) // Send the current greeting back to the sender
}
}
这尤其是位:
def receive = {
case WhoToGreet(who) => greeting = s"hello, $who"
case Greet => sender ! Greeting(greeting) // Send the current greeting back to the sender
}
现在我想这种情况下,语法斯卡拉看起来是这样的:
something match {
case "val1" => println("value 1")
case "val2" => println("value 2")
}
如果我尝试复制使用情况在斯卡拉REPL这样的问题:
def something = {
case "val1" => println("value 1")
case "val2" => println("value 2")
}
我得到
error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)
究竟是什么意思?
UPDATE:这篇文章是迄今为止最好的回答我的问题:http://blog.bruchez.name/2011/10/scala-partial-functions-without-phd.html
那么我明白,用'匹配'它的工作原理,但我的问题仍然存在 - 为什么它没有'匹配'的Akka示例工作?我找不到任何在线语法的参考。 – Caballero
对不起,我完全错过了你正在寻求澄清。编辑在一个答案:) – Damiya
@Caballero它与'receive'一起工作的原因是因为你重写它,并且父类型'Actor'定义了函数的类型,它是'PartialFunction ...'。要定义您自己的部分函数,您只需定义正确的返回类型(PartialFunction)。有关示例,请参阅文档:http://www.scala-lang.org/api/2.10.3/index.html#scala.PartialFunction –