这是一个涉及akka的代码:
def receive = {
case idList: List[ActorRef] => idList.foreach(x => x ! Msg)
}
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
我怎么摆脱这个?
在运行时List[Whatever]相当于List[Any],所以你的Actor可以弄清楚它收到了一个列表但不是它的列表ActorRef.这是一个JVM的东西,而不是Scala或Akka的错.
你有两个选择:
1)通过替换ActorRef来忽略它_
case idList: List[_] => ...
Run Code Online (Sandbox Code Playgroud)
2)将其包裹到数据结构中(推荐)
case class Ids(idList: List[ActorRef])
Run Code Online (Sandbox Code Playgroud)
第二个选择让你检查ID而不必检查列表的参数类型.
def receive = {
case Ids(idList) => idList.foreach(x => x ! Msg)
}
Run Code Online (Sandbox Code Playgroud)