简体   繁体   English

Scala案例语法理解

[英]Scala case syntax understanding

I'm trying to get a grip on Scala's actors (Akka), but I just came across some weird bit of "case" usage that I don't understand: 我试图控制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
    }

}

This bit in particular: 这一点特别:

  def receive = {
      case WhoToGreet(who) => greeting = s"hello, $who"
      case Greet           => sender ! Greeting(greeting) // Send the current greeting back to the sender
  }

Now I thought that case syntax in scala looks like this: 现在我认为scala中的case语法如下所示:

something match {
    case "val1" => println("value 1")
    case "val2" => println("value 2")
}

If I try to replicate the usage in question in scala REPL like this: 如果我尝试在scala REPL中复制有问题的用法,如下所示:

def something = {
    case "val1" => println("value 1")
    case "val2" => println("value 2")
}

I get 我明白了

error: missing parameter type for expanded function
The argument types of an anonymous function must be fully known. (SLS 8.5)

What exactly does this mean? 这到底是什么意思?

UPDATE: This article is by far the best answer to my question: http://blog.bruchez.name/2011/10/scala-partial-functions-without-phd.html 更新:这篇文章是我的问题的最佳答案: http//blog.bruchez.name/2011/10/scala-partial-functions-without-phd.html

Case Syntax in scala has a number of forms it can take. scala中的Case语法可以采用多种形式。

Some examples are: 一些例子是:

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")

The reason you're seeing that error is because you didn't attempt to invoke match on a known object, but instead tried to assign a case block to a function. 您看到该错误的原因是您没有尝试在已知对象上调用匹配 ,而是尝试将case块分配给函数。

Because a Case block is just a PartialFunction deep down, the compiler thinks you're trying to define a partial function but not giving it enough info because there's nothing for it to apply to. 因为Case块只是一个PartialFunction内部,编译器认为你正在尝试定义一个部分函数但没有给它足够的信息,因为它没有任何东西可以应用。

Try something like: 尝试类似的东西:

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

And you'll be able to invoke it. 而且你将能够调用它。

Edit 编辑

The case in the Akka example works because you're actually providing an implementation for an abstract, pre-existing function: receive is defined in akka.actor.Actor. Akka示例中的情况有效,因为您实际上是为抽象的,预先存在的函数提供了一个实现:receive在akka.actor.Actor中定义。 You can see the typedef in the Akka source : 您可以在Akka源中看到typedef:

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

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

The receive call in the example is compiled to 示例中的receive调用被编译为

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

Which tells us that Receive will take any value or reference and return a unit. 这告诉我们接收将采用任何值或参考并返回一个单位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM