is it possible to overcome type erasure in akka receive?

Viewed 1132

I have a trait and implementing akka actor that can't properly differentiate the types of incoming messages due to type erasure, and so the first case is matching all messages.

I'm using scala 2.10.x, and from reading many other answers I know that it is possible to recover type information inside a trait using TypeTag or ClassTag, but I can't figure out how to apply it (if it is possible) within the akka receive.

My (very simplified) example is as follows. Is it possible properly match the generic types?

package com.ebay.box.canada.batch.jobs.siteMap

import akka.actor.Actor
import akka.actor.ActorSelection
import akka.actor.Actor.Receive
import scala.reflect.ClassTag


trait MessageProcessor[A,B] {
  this: Actor =>

  val destA: ActorSelection
  val destB: ActorSelection

  def processA(a: A): A
  def processB(a: B): B

  def receive: PartialFunction[Any,Unit] = {
    case a: A =>
      destA ! processA(a)
    case b: B =>
      destB ! processB(b)
  }
}

class StringIntProcessor(val destA: ActorSelection, val destB: ActorSelection) extends MessageProcessor[String,Int] with Actor {
  def processA(a: String) = { a + "1" }
  def processB(b: Int) = { b + 1 }
}
1 Answers
Related