Created
December 12, 2013 06:02
-
-
Save nairbv/7923811 to your computer and use it in GitHub Desktop.
An example of using a Manifest to get around type erasure
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
scala> def foo[T : Manifest](a : List[T]) = { | |
| val m = implicitly[Manifest[T]] | |
| val I = classOf[Int] | |
| val S = classOf[String] | |
| m.erasure match { | |
| case I => "hi" | |
| case S => "bye" | |
| case _ => "whatever" | |
| } | |
| } | |
foo: [T](a: List[T])(implicit evidence$1: Manifest[T])java.lang.String | |
scala> foo(List(1,2,3)) | |
res6: java.lang.String = hi | |
scala> foo(List("blah")) | |
res7: java.lang.String = bye | |
scala> foo(List(1.2)) | |
res8: java.lang.String = whatever | |
scala> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment