我试图动态过滤器(或收集)的列表基于类型类型:使用与收集
如果我这样做明确指定类型,它工作正常
scala> var aList = List("one", 2, 3.3)
aList: List[Any] = List(one, 2, 3.3)
scala> aList.collect{case x:Int => x}
res10: List[Int] = List(2)
如果我要编写一个方法一般地做到这一点,那么它不会:
scala> def collectType[T](l:List[Any]):List[T] = l.collect{case x:T => x}
warning: there were unchecked warnings; re-run with -unchecked for details
collectType: [T](l: List[Any])List[T]
scala> collectType[Int](aList)
res11: List[Int] = List(one, 2, 3.3)
scala> collectType[Double](aList)
res16: List[Double] = List(one, 2, 3.3)
scala> collectType[String](aList)
res14: List[String] = List(one, 2, 3.3)
我开始还以为它被命名类型“整型”,而不是使用整数作为类型,但是这似乎并没有情况如下:
collectType[Int](aList).foreach(x => println(x))
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
就好像它的推迟,直到它被迫
我缺少的是关于类型检查的类型?
有没有办法实现我想达到的目的?
通过阅读链接的问题后,这就是我想出的。现在已经指出它很简单。加标签是它知道如何把握地图上标记为一类
def matches[F <: Taggable](thing:Taggable)(implicit m:Manifest[F]):Boolean = {
thing match {
case e if (m >:> singleType(e)) => true
case x => false
}
}
def findByType[G <: Taggable](list:List[Taggable])(implicit m:Manifest[G]) = {
list.collect{case x if (matches[G](x)) => x}
}
'警告:有未经检查的警告;用-unchecked查看详细信息,然后重新运行 – soc