I\'m trying to figure out how to invoke a constructor for a Scala abstract type:
class Journey(val length: Int)
class PlaneJourney(length: Int) extends Journey(l
Your class needs an implicit constructor parameter to get the Manifest
. Then you can call erasure to get the Class
and call newInstance
, which reflectively calls the nullary constructor if there is one.
class J[A](implicit m:Manifest[A]) {
def n = m.erasure.newInstance()
}
new J[Object].n
As of Scala 2.10, the erasure
property in the manifest is deprecated. def n = m.runtimeClass.newInstance()
does the same thing, but without warnings.