Here below is how to create a new instance of type T at runtime with Manifest:
trait MyTrait
class MyClass1(val name: String) extends MyTrait
cl
Eventually here is the implementation that actually works - please just assume this answer comes from som-snytt:
import scala.reflect.runtime._
import scala.reflect.runtime.universe._
class Test[T <: MyTrait : TypeTag] {
def createInstance(args: AnyRef*)(ctor: Int = 0): T = {
val tt = typeTag[T]
currentMirror.reflectClass(tt.tpe.typeSymbol.asClass).reflectConstructor(
tt.tpe.members.filter(m =>
m.isMethod && m.asMethod.isConstructor
).iterator.toSeq(ctor).asMethod
)(args: _*).asInstanceOf[T]
}
}
I hope that helps.