【发布时间】:2016-05-12 22:18:26
【问题描述】:
我想去掉这个示例代码的类型擦除警告:
val a: Seq[Any] = Seq(1)
a match {
case b: Seq[Int] => b.map(c => println(2 * c) )
case _ => println("Not matched")
}
它编译和工作正常,但有一个警告:
警告:(31, 13) 类型模式中的非变量类型参数 Int Seq[Int](Seq[Int] 的底层)是未选中的,因为它是 通过擦除消除 案例b: Seq[Int] => b.map(c => println(2 * c) ) ^
您有什么简单的解决方案可以避免这种情况下的擦除吗?
到目前为止我尝试了什么(根据this):
val a: Seq[Any] = Seq(1)
a match {
case b@Seq(_:Int) => b.map(c => println(2 * c) )
case _ => println("Not matched")
}
但它不会编译,因为 c 现在是 Any 类型。
我相信这个问题有几种解决方案。我会接受最简单的。
【问题讨论】:
-
请注意,您的方法破坏了参数化 - dl.dropboxusercontent.com/u/7810909/talks/parametricity/…。
标签: scala pattern-matching type-erasure type-parameter generic-type-argument