Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created June 24, 2011 21:48
Show Gist options
  • Save kmizu/1045765 to your computer and use it in GitHub Desktop.
Save kmizu/1045765 to your computer and use it in GitHub Desktop.
Usage of ClassManifest
def matchType[A:ClassManifest](value: Any): Boolean = {
val classAny: Class[_] = value match {
case _:Byte=> classOf[Byte]
case _:Short => classOf[Short]
case _:Int => classOf[Int]
case _:Long => classOf[Long]
case _:Float => classOf[Float]
case _:Double => classOf[Double]
case _:Char => classOf[Char]
case _:Boolean => classOf[Boolean]
case _:Unit => classOf[Unit]
case _ => value.asInstanceOf[AnyRef].getClass
}
val classA = implicitly[ClassManifest[A]].erasure.asInstanceOf[Class[A]]
classAny.isAssignableFrom(classA)
}
def f[T:ClassManifest](h:Seq[Any]) = h.flatMap{
case a if matchType[T](a) =>Some(a); case _=>None
}
println(f[String](List(1, 2, 3)))
println(f[Int](List(1, 2, 3)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment