首页 文章

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

提问于
浏览
2

我正在为我的 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

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

1 回答

  • 3

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

    你可以尝试一下的方法是将 Seq[Id] 包装在案例类中 .

    case class MyAwesomeSeq(s: Seq[Id])
    

    和模式匹配 .

相关问题