这是一个涉及akka的代码:
def receive = {
case idList: List[ActorRef] => idList.foreach(x => x ! Msg)
}
Sbt抱怨说:
non-variable type argument akka.actor.ActorRef in type pattern List[akka.actor.ActorRef] is unchecked since it is eliminated by erasure
[warn] case idList: List[ActorRef] => idList.foreach(x => x ! Msg)
如何摆脱这种情况?
答案 0 :(得分:6)
在运行时List[Whatever]
相当于List[Any]
,因此您的Actor可以确定它收到了一个列表但不是它是ActorRef
的列表。这是JVM的事情,而不是Scala或Akka的错。
您有两种选择:
1)将ActorRef
替换为_
case idList: List[_] => ...
2)将其包装到数据结构中(推荐)
case class Ids(idList: List[ActorRef])
第二个选择让你检查ID而不必检查列表的参数类型。
def receive = {
case Ids(idList) => idList.foreach(x => x ! Msg)
}