当前位置: 首页 > 工具软件 > variable-type > 使用案例 >

warning: non-variable type argument Any in type pattern scala.collection.immutable.Set[Any] (the und

隗昀
2023-12-01

scala> suppdata
res91: Array[Any] = Array((Set(5),1.0), (Set(3),1.0))

scala> suppdata.map(_ match{case a:Tuple2[Any,Any] => a._1})
res92: Array[Any] = Array(Set(5), Set(3))

scala> suppdata.map(_ match{case a:Tuple2[Set[Int],Int] => a._1})
<console>:48: warning: non-variable type argument Set[Int] in type pattern (Set[Int], Int) is unchecked since it is eliminated by erasure
       suppdata.map(_ match{case a:Tuple2[Set[Int],Int] => a._1})
                                   ^
res93: Array[Set[Int]] = Array(Set(5), Set(3))

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> suppdata.map(_ match{case a:Tuple2[Set[Int] @unchecked,Int @unchecked] => a._1})
res94: Array[Set[Int]] = Array(Set(5), Set(3))

scala> suppdata.map(_ match{case a:Tuple2[_,_] => a._1 match{ case b:Set[_] => b}})
res95: Array[scala.collection.immutable.Set[_]] = Array(Set(5), Set(3))

scala> suppdata.map(_ match{case a:Tuple2[_,_] => a._1 match{ case b:Set[_] => b.asInstanceOf[Set[Int]]}})
res96: Array[Set[Int]] = Array(Set(5), Set(3))

scala> 

参考:

http://stackoverflow.com/questions/1094173/how-do-i-get-around-type-erasure-on-scala-or-why-cant-i-get-the-type-paramete

http://stackoverflow.com/questions/25222989/erasure-elimination-in-scala-non-variable-type-argument-is-unchecked-since-it

 类似资料: