【问题标题】:The Actor DSL example from Akka docAkka 文档中的 Actor DSL 示例
【发布时间】:2013-07-25 07:37:18
【问题描述】:

我尝试从 akk doc 实现 Actor DSL 示例,但发现错误,

不明确的隐含值:在 trait Inbox 类型的两个方法 senderFromInbox(隐含收件箱: akka.actor.ActorDSL.Inbox)akka.actor.ActorRef 并在类型的 trait Actor 中为 self 赋值 => akka.actor.ActorRef 匹配预期类型 akka.actor.ActorRef

下面是我的代码,

import akka.actor.ActorDSL._
import akka.actor.ActorSystem
import scala.concurrent.duration._
implicit val system: ActorSystem = ActorSystem("demo")

implicit val i = inbox()
val a = actor(new Act {
  become {
    case "hello" ⇒ sender ! "hi"
  }
})
a ! "hello"
val reply = i.receive()

这里我不能使用“!”要发送消息,只能使用 sender.tell("hi", null) 之类的“tell”,有人知道如何解决这个问题吗?

【问题讨论】:

    标签: scala akka


    【解决方案1】:

    简答(仅适用于没有:paste 模式的 REPL):

    val a = ...
    implicit val i = inbox()
    

    您应该传递self,而不是null 作为tell 方法的第二个参数(sender)。方法! 隐式获取此参数并调用tell。 sender ! "hi" 范围内有 2 个隐式 ActorRef:i 和 self(Act 的字段) - 编译器无法确定您需要哪一个。

    您应该从sender ! "hi" 的范围中删除implicit val i。

    正确的解决方案 - 将 Actor 创建移至方法和所有其他代码 - 移至其他方法。在 REPL 中,您可以在 i 之前创建 a。

    快速肮脏的解决方案 - 像这样隐藏i:

    val a = {
      val i = 0
      actor(new Act {
      ...
    }
    

    【讨论】:

    • 正确。对于这个测试代码,我只是想测试DSL功能,所以不要把self告诉方法,是的,应该放
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 2016-07-18
    • 2011-02-25
    • 1970-01-01
    相关资源
    最近更新 更多