0
我有一個用2個構造函數創建的類。在scala中使用多重構造函數創建一個實例
我想使用默認構造函數創建此類的實例。
class A (arg1:Int,Arg2:String){
def this(arg1:Int){
this(arg1,"hello")
}
}
這裏是我嘗試這樣做:
val tpe = ru.typeOf[A]
val mirror = ru.runtimeMirror(getClass.getClassLoader)
val clsSym = tpe.typeSymbol.asClass
val clsMirror = mirror.reflectClass(clsSym)
val ctorSym = tpe.decl(ru.termNames.CONSTRUCTOR).asMethod
val method = clsMirror.reflectConstructor(ctorSym)
val newInstance = method(args:_*)
,我發現了以下錯誤:
constructor ExampleWithCamelCase encapsulates multiple overloaded
alternatives and cannot be treated as a method. Consider invoking
<offending symbol>.asTerm.alternatives` and manually picking the required method
是有沒有去選擇默認的構造函數?