【问题标题】:Where is akka class Receive defined?akka 类 Receive 定义在哪里?
【发布时间】:2014-09-12 00:55:35
【问题描述】:

我对 Scala 还是很陌生。

在其中一个 Actor 类中,我看到我的同事定义这样的代码

导入 akka.actor.Actor

class Processor extends Actor {

  def receive: Receive = {
    case msg: String => doProcess(msg)
    case _ => 
  }
}

这个类 Receive 是在哪里定义的?它没有被导入到这个类中。系统如何找到 Receive 类

【问题讨论】:

  • 你为什么不接受答案?...

标签: scala akka


【解决方案1】:

它是PartialFunction[Any, Unit] 的类型别名,分别在akka.actor.Actor 伴随对象和特征中定义。

摘自source code

object Actor {
    // Type alias representing a Receive-expression for Akka Actors.
    type Receive = PartialFunction[Any, Unit]
    // ...
}

trait Actor {
    // to make type Receive known in subclasses without import
    type Receive = Actor.Receive
    // ...
}

【讨论】:

    【解决方案2】:

    ReceivePartialFunction[Any,Unit] 的类型别名。此类型别名在 Actor 伴随对象上定义,然后在 Actor 特征上重新定义(因此在 Actor impls 中可用)指的是在伴随对象上定义的那个。

    【讨论】:

      【解决方案3】:

      Receive 是 PartialFunction[Any, Unit] 的类型别名

      查看Actor源码here

      你也可以把receive方法写成

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-08
        • 2012-08-31
        • 1970-01-01
        • 1970-01-01
        • 2016-09-08
        • 2011-05-02
        • 2014-06-01
        相关资源
        最近更新 更多