Scala:没有进行隐式转换?

Scala: implicit conversion not made?

[error] DeviceAffiliationCluster.scala:56: value ask is not a member of akka.actor.ActorRef
[error]   def ask(msg: Any): Future[Any] = deviceRegion.ask(msg)
[error]                                                 ^
[warn] DeviceAffiliationCluster.scala:5: Unused import
[warn] import akka.pattern.ask

akka.pattern.ask 提供隐式转换(从 ActorRefAskableActorRef,后者提供方法 ask

但是,当我使用 sbt 进行编译时,无法识别转换。 (Intellij 看到隐式转换并且没有问题,但我正在使用 sbt 构建。)

我可以明确地让它工作:

val deviceRegion: ActorRef =  ...

val deviceRegionAskable: AskableActorRef = deviceRegion

问题是您的方法 ask 隐藏了您从 akka.pattern.ask 导入的 ask 方法 如果您使用不同的方法名称那么您的示例工作正常

  import akka.actor._
  import akka.pattern.ask
  import scala.concurrent.duration._
  import scala.concurrent._

  class FooActor extends Actor {
    def receive = {
    case s: String => sender ! s"Hello $s"
    }}
  val ac = ActorSystem()
  implicit val ec : ExecutionContext = ac.dispatcher
  val fooAc = ac.actorOf(Props[FooActor], "fa")
  implicit val to = new akka.util.Timeout(10 seconds)
  def ask2(msg: Any) : Future[Any] = fooAc.ask("foo")
  val x = Await.result(ask2("foo"), Duration.Inf)
  println(x)