Skip to content

Instantly share code, notes, and snippets.

@samueltardieu
Created July 29, 2011 20:17
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save samueltardieu/1114634 to your computer and use it in GitHub Desktop.
Type erasure and reification
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