Created
June 24, 2011 21:48
-
-
Save kmizu/1045765 to your computer and use it in GitHub Desktop.
Usage of ClassManifest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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