scala - What's the type of a class inside an object inside a trait -
this problem of pattern matching inside recieve of akka actor.
i have code:
class actorreservation(reservation: reservation) extends actor { import entry._ import customer._ def receive: actor.receive = messages def messages: receive = { case entry.get.result(obj) => // process entry case customer.get.result(obj) => // process customer } def test: receive = { case e => println(s"classname -> ${e.getclass.getname}") println(s"isinstanceof[genericmessages#get] -> ${isinstanceof[genericmessages[appany]#get]}") } } object entry extends genericmessages[entryentity] object customer extends genericmessages[customerentity] trait genericmessages[a <: appany] { case class get(id: int) object { case class result(obj: option[a]) } } //trait trait appany case class entryentity(id: int) extends appany case class customerentity(id: int) extends appany my actor receive 2 messages: entry.get.result, customer.get.result, when receives first 1 goes find, when second arrives throws , exception customer can cast entry, because second messages been caught first case.
then change receive with
def receive: actor.receive = test both message shows same class name genericmessages$get$result, thats why both messages falls in first case.
i try create generic case both messages genericmessages#get makes references class get inside genericmessages instead of object get.
so, question is: there way reference result class inside get object of genericmessages trait? there way make case avoid pattern match problem?
additional information: real actor implementation of generic actor because need same behavior in other actors.
the messages entry.get.result, customer.get.result responses of previous message send same actor (anotheractor ! customer.get) , implemented in way because prefer parallel calls linear calls. linear solution have 1 method receive first message use become change receive , process second message. want avoid linear approach because 1. prefer parallel process , 2. breaks abstraction made.
there 1 solution (which ugly): make entry , customer classes:
case class entry() extends genericmessages[entryentity] case class customer() extends genericmessages[customerentity] then create entry , customer instance:
val entry = entry() val customer = customer() and pass them parameters actorreservation actor (and anybidy needs it). create , match this:
entry.get.result customer.get.result but bad design.
i suggest avoiding generics when working actors. type erasure becomes problem when need match types @ runtime. if you, redo design.
Comments
Post a Comment