2011-09-05 77 views
3

我试图动态过滤器(或收集)的列表基于类型类型:使用与收集

如果我这样做明确指定类型,它工作正常

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} 
} 
+0

'警告:有未经检查的警告;用-unchecked查看详细信息,然后重新运行 – soc

回答

7

你缺少type erasure一个特点。在运行时,您的方法实际上是

def collectType(l:List):List = l.collect {case x:Object => x} 
+0

谢谢,是的,我终于发现了一个类似于我的问题(我在寻找真实信息之前曾经搜索过很多信息,但没有找到正确的单词组合)http:// stackoverflow。 com/questions/3660217/scala-collection-filter-by-type – CPJ

+0

Alexey的答案中的链接还表明,您可以通过将类型重新定义为运行时“Manifest”对象来解决类型删除问题。 –

+0

@CPJ:最好把它添加到你的问题的最后。评论中的多行代码相当难以理解。 –