Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reflection 在scala 2.10中通过反射调用构造函数_Reflection_Constructor_Scala 2.10 - Fatal编程技术网

Reflection 在scala 2.10中通过反射调用构造函数

Reflection 在scala 2.10中通过反射调用构造函数,reflection,constructor,scala-2.10,Reflection,Constructor,Scala 2.10,在scala 2.10(M4+)中调用类构造函数的最佳实践是什么?回答我自己的问题: 调用构造函数与调用方法不同。下面是在scala 2.10中执行此操作的正确方法 import reflect.runtime.universe._ import reflect.runtime.currentMirror val typ = typeOf[Range] val constructor = typ.members.find(_.kind == "constructor").get.asMethod

在scala 2.10(M4+)中调用类构造函数的最佳实践是什么?

回答我自己的问题: 调用构造函数与调用方法不同。下面是在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)
正如预期的那样,结果是:

res7: Any = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)