问题
What's the best practice for calling a constructor of a class in scala 2.10 (M4+) ?
回答1:
Answering my own question: Calling the constructor is different than invoking a method. Here's the right way to do it in scala 2.10
import reflect.runtime.universe._
import reflect.runtime.currentMirror
val typ = typeOf[Range]
val constructor = typ.members.find(_.kind == "constructor").get.asMethodSymbol
currentMirror reflectClass typ.typeSymbol.asClassSymbol reflectConstructor constructor apply (1,10,1)
As expected, the result is:
res7: Any = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
来源:https://stackoverflow.com/questions/11755735/calling-a-constructor-through-reflection-in-scala-2-10