Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Actor DSL example from Akka doc

Tags:

scala

akka

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

ambiguous implicit values: both method senderFromInbox in trait Inbox of type (implicit inbox: akka.actor.ActorDSL.Inbox)akka.actor.ActorRef and value self in trait Actor of type => akka.actor.ActorRef match expected type akka.actor.ActorRef

below is my code,

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()

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?

like image 581
taigetco Avatar asked Dec 17 '25 17:12

taigetco


1 Answers

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

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

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.

You should remove implicit val i from scope of sender ! "hi".

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

Quick dirty solution - hide i like this:

val a = {
  val i = 0
  actor(new Act {
  ...
}
like image 113
senia Avatar answered Dec 19 '25 12:12

senia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!