Created
June 16, 2011 22:45
-
-
Save kings13y/1030485 to your computer and use it in GitHub Desktop.
Reification via a Manifest example
This file contains 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
class ReifiedManifest[T <: Any : Manifest](value: T) { | |
val m = manifest[T] // So at this point we have the manifest for the Parameterized type | |
// At which point we could either do an if() expression on what type is contained in our manifest | |
if (m equals manifest[String]) { | |
println("The manifest contains a String") | |
} else if (m <:< manifest[AnyVal]) { // A subtype check using the <:< operation on the Manifest trait | |
println("The manifest contains a subtype of AnyVal") | |
} else if (m <:< manifest[AnyRef]) { | |
println("The manifest contains a subtype of AnyRef") | |
} else { | |
println("Not sure what type is contained ?") | |
} | |
// or we could grab the erased type from the manifest and do a match on some attribute of the type | |
m.erasure.toString match { | |
case "class java.lang.String" => println("ERASURE: pattern matches on a String") | |
case "double" | "int" => println("ERASURE: pattern matches on a Numeric value.") | |
case x => println("ERASURE: has picked up another type not spec'd in the pattern match: " + x) | |
} | |
} | |
new ReifiedManifest("Test") // Contains a String / matches on a String | |
new ReifiedManifest(1) // Contains an AnyVal / matches on a Numeric | |
new ReifiedManifest(1.2) // Contains an AnyVal / matches on a Numeric | |
new ReifiedManifest(BigDecimal("3.147")) // Contains an AnyRef / matches on a an unspecified type |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment