Question

Using Akka 2.1.0 I'm sending a message from one Actor (ActorA) to another (ActorB) and expecting the returned message to be an Option[(String, String)]. ActorB has a val defined as an Enumeration and this is being returned as the second element of the Tuple result. ActorA uses the Await.result().asInstanceOf[Option[(String, String)] pattern (I know this is bad blocking behavior, but don't think this explains the behavior I'm seeing), and assigns the result to a val. Later when I try to pull out the second element of the Tuple result I get a scala.Enumeration#Val cannot be cast to java.lang.String cast exception. In previous versions of Akka this did not appear, and in Akka 2.1.0 I would expect the Await.result operation to blow up. Can anyone explain what is going on here?

object MyEnumeration extends Enumeration {
  type Enum = Value
  val Foo = Value("foo")
  val Bar = Value("bar")
}

case class ActorA extends Actor {
  implicit val timeout = Timeout(10000)
  val result = Await.result((ActorB ? MyMessage), timeout.duration).asInstanceOf[Option[(String, String)]]

  val validResult = result.get
  val validType = validResult._2 // this is of type Enumeration#Val not String

}

case class ActorB extends Actor {
  def myType = MyEnumeration.Foo
  def receive = {
  case MyMessage =>
      sender ! Option((validString, myType))
  }
}
Was it helpful?

Solution

It didn't actually work in Akka 2.0.3. As I understood, you parsed received message differently.

// enum
object E extends Enumeration { val a,b,c = Value }

// send 
case "Hello" => sender ! Some(("Hai", E.a))

// works
Await.result(rootActor ? "Hello", timeout.duration)
.asInstanceOf[Option[(String, String)]] match {
  case a: Option[Tuple2[_, _]] => // diff is here 
    println("" + a.get._2)
    a.get._2
}  


// does not work
Await.result(rootActor ? "Hello", timeout.duration)
.asInstanceOf[Option[(String, String)]] match {
  case a: Option[Tuple2[String, String]] => // diff is here
    println("" + a.get._2)
    a.get._2
}  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top