Search code examples
scalaakkaimplicits

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 supplies an implicit conversion (from ActorRef to AskableActorRef, the latter which provides the method ask)

When I compile using sbt, however, the conversion is NOT recognized. (Intellij sees the implicit conversion and has no issue with it, but I'm using sbt to build.)

I can get it to work explicitly:

val deviceRegion: ActorRef =  ...

val deviceRegionAskable: AskableActorRef = deviceRegion

Solution

  • the problem is that your method ask is hiding the ask method which you imported from akka.pattern.ask If you use a different method name then your example works fine

      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)