Scala案例语法理解

时间:2014-02-08 13:02:49

标签: scala akka

我正试图控制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

1 个答案:

答案 0 :(得分:3)

scala中的Case语法可以采用多种形式。

一些例子是:

case personToGreet: WhoToGreet => println(personToGreet.who)
case WhoToGreet(who) => println(who)
case WhoToGreet => println("Got to greet someone, not sure who")
case "Bob" => println("Got bob")
case _ => println("Missed all the other cases, catchall hit")

您看到该错误的原因是您没有尝试在已知对象上调用匹配,而是尝试将一个案例块分配给一个函数。

因为Case块只是一个PartialFunction,所以编译器认为你正在尝试定义一个部分函数,​​但没有给它足够的信息,因为它没有任何内容可以应用。

尝试类似:

def something: PartialFunction[Any,Unit] = {
    case "val1" => println('value 1")
    case _ => println("other")
}

你可以调用它。

修改

Akka示例中的情况有效,因为您实际上是为抽象的,预先存在的函数提供了一个实现:receive在akka.actor.Actor中定义。您可以在Akka source中找到typedef:

object Actor {
  type Receive = PartialFunction[Any, Unit]
  ..
}

trait Actor {
  def receive: Actor.Receive 
  ....
} 

示例中的接收调用编译为

def receive: PartialFunction[Any, Unit] = {
}

告诉我们接收将采用任何值或引用并返回单位。