【发布时间】:2014-02-08 13:02:48
【问题描述】:
我正在尝试了解 Scala 的演员 (Akka),但我只是遇到了一些我不明白的奇怪的“案例”用法:
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
}
现在我认为 scala 中的 case 语法如下所示:
something match {
case "val1" => println("value 1")
case "val2" => println("value 2")
}
如果我尝试像这样在 scala 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)
这到底是什么意思?
更新:这篇文章是我问题的最佳答案:http://blog.bruchez.name/2011/10/scala-partial-functions-without-phd.html
【问题讨论】: