因为擦除而被删除,所以不加以控制

时间:2015-10-31 16:07:38

标签: scala akka type-erasure

我正在为我的Akka演员编写测试,我的演员使用Seq[Id]进行回复(而Idcase 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

在没有警告的情况下让它工作的方法是什么?

1 个答案:

答案 0 :(得分:3)

Scala是使用Type Erasure定义的。在运行时,JVM只会看到Seq而不是其类型参数。

如果将Seq[Id]包装在案例类中,可以采取一种方法。

case class MyAwesomeSeq(s: Seq[Id])

与模式匹配。