[英]scala : no common ancestor available for asInstanceOf[]
I created some case classes used as messages in akka. 我创建了一些case类用作akka中的消息。 When the program receives some messages, it calls the method asInstanceOf[]
, but I don't know what to put inside the brackets, as the message can be one of the 3 differents case classes. 当程序接收到一些消息时,它会调用方法asInstanceOf[]
,但是我不知道该放在方括号中的内容,因为该消息可能是3种不同的case类之一。
here is a try : 这是一个尝试:
abstract class Retour
case class Naissance(val sender : ActorRef) extends Retour
case class NaissanceEtMort(val sender : ActorRef) extends Retour
case class PlusUnMoisOK(val sender : ActorRef) extends Retour
but at the execution of my program I get this error message: 但是在执行程序时,我收到以下错误消息:
lapins.Clapier$PlusUnMoisOK$ cannot be cast to lapins.Clapier$Retour
can you help me? 你能帮助我吗?
EDIT: 编辑:
the line of the error is those with [Retour] in : 错误所在的行是那些带有[Retour]的行:
val future = ask(couple,PlusUnMois)
val result = Await.result(future,timeout.duration).asInstanceOf[Retour]
and the error is below: 错误如下:
[ERROR] [07/02/2015 01:33:48.436] [clapier-akka.actor.default-dispatcher-5] [akka://clapier/user/$a] lapins.Clapier$PlusUnMoisOK$ cannot be cast to lapins.Clapier$Retour
akka.actor.ActorInitializationException: exception during creation
EDIT2: 编辑2:
resolved! 解决! the message sending was incorrectly written : indeed, I wrote "sender ! PlusUnMoisOK" instead of "sender ! PlusUnMoisOK(self)" 发送的消息写错了:确实,我写的是“ sender!PlusUnMoisOK”,而不是“ sender!PlusUnMoisOK(self)”
You could pattern match on the result of the future. 您可以根据未来的结果进行模式匹配。 Something like: 就像是:
Await.result(future, timeout.duration) match {
case n: Naissance => // ...
case nm: NaissanceEtMort => // ...
case pm: PlusUnMoisOK => // ...
}
With: 带有:
ask(couple,PlusUnMois)
You are sending the class PlusUnMois
as a message. 您正在将类PlusUnMois
作为消息发送。 If you are doing the same in your couple
actor Await.result
is returning the class PlusUnMois
not an object of PlusUnMois
. 如果你正在做同样的在你的couple
的演员Await.result
还是返回了类PlusUnMois
不是一个对象PlusUnMois
。 Trying to cast the class PlusUnMois
to Retour
results to an error. 试图将类PlusUnMois
为Retour
导致错误。 Try sending an instance of PlusUnMois
, like: 尝试发送PlusUnMois
的实例,例如:
val future = ask(couple,new PlusUnMois(someActorRef))
The same should be done in your couple
actor: 在您的couple
演员中也应这样做:
sender ! new PlusUnMois(someOtherActorRef)
Moreover, instead of using asInstanceOf
consider using pattern matching: 此外, asInstanceOf
使用asInstanceOf
考虑使用模式匹配:
Await.result(future,timeout.duration) match {
case m : Naissance => // do something Naissance
case m : NaissanceEtMort => // do something NaissanceEtMort
case m : PlusUnMoisOK => // do something PlusUnMoisOK
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.