我正在为我的Akka
参与者编写测试,其中我的参与者用Seq[Id]
(而Id
是我的case class
)进行响应。
我做
val generator = TestActorRef[IdGenerator]
val batchSize: Int = 10
within(10.millis) {
generator ! GetIdentifiers(batchSize)
expectMsgPF() {
case ids: Seq[Id] => println(ids)
}
}
当我编译代码时,我会收到这样的警告:
[info] Compiling 1 Scala source to /Users/harit/IdeaProjects/identity/target/scala-2.11/test-classes...
[warn] /Users/harit/IdeaProjects/identity/src/test/scala/com/identity/business/IdGeneratorSpec.scala:32: non-variable type argument com.identity.message.Id in type pattern Seq[com.identity.message.Id] (the underlying of Seq[com.identity.message.Id]) is unchecked since it is eliminated by erasure
[warn] case ids: Seq[Id] => println(ids)
[warn] ^
[warn] one warning found
什么方法可以让它在没有警告的情况下工作?
Scala是用类型擦除定义的。在运行时,JVM将只看到一个Seq
,而不是它的类型参数。
可以绕过它的一种方法是,将Seq[Id]
封装在case类中。
case class MyAwesomeSeq(s: Seq[Id])
并且模式与之匹配。