Akka Doc中的Actor DSL示例 [英] The Actor DSL example from Akka doc

查看:135
本文介绍了Akka Doc中的Actor DSL示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to implement Actor DSL example from akk doc, but found error,

模糊的隐式值:这两种方法senderFromInbox都在特征类型Inbox中(隐式)收件箱:
akka.actor.ActorDSL.Inbox)akka.actor.ActorRef和value self in trait Actor类型=>
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,有人知道如何解决此问题吗?

here I can't use "!" to send message, only can use "tell" like sender.tell("hi", null), does anybody know how to fix this issue?

推荐答案

简短答案(仅适用于不具有:paste 模式的REPL):

Short answer (only for REPL without :paste mode):

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

您应该将 self 而不是 null 作为第二个参数(发件人)的 tell 方法。方法隐式采用此参数并调用 tell 。在 sender范围内有2个隐式 ActorRef ! hi i self Act的字段)-编译器无法确定您需要哪一个。

You should pass self, not null as second parameter (sender) of tell method. Method ! takes this parameter implicitly and invokes tell. There are 2 implicit ActorRef in scope of sender ! "hi": i and self (field of Act) - compiler can't figure out which one you need.

您应删除隐式val i 来自 sender的范围! hi

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

Correct solution - move actor creation to method and all other code - to other method. In REPL you could create a before i.

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

Quick dirty solution - hide i like this:

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

这篇关于Akka Doc中的Actor DSL示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆