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)