Type erasure and reification
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 Example { | |
println("Constructing an Example object: " + this) | |
} | |
object Test extends App { | |
// Return a new object of type T when called as f[T] | |
def f[T : Manifest] = manifest[T].erasure.getConstructor().newInstance() | |
// Return a string with the name of the class when called as name[T] | |
def name[T : Manifest] = manifest[T].toString | |
// This will print something like: | |
// Example | |
// Constructing an Example object: Example@74b23210 | |
// Example@74b23210 | |
println(name[Example]) | |
println(f[Example]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment