Search code examples
scalareflectioninstantiationcurrying

scala reflection instantiate class with multiple paramLists


Assume we have class A

class A(val x: Any)(val y: Any)

then we want to instantiate it reflectively having x & y values:

def instantiate[T](aClass: Class[T], args: List[Any]): T = {
    val mirror = ru.runtimeMirror(aClass.getClassLoader)
    val classMirror = mirror.reflectClass(mirror.classSymbol(aClass))
    val constructorMirror = classMirror.reflectConstructor(getPrimaryConstructor(aClass))
    constructorMirror.apply(args: _*).asInstanceOf[T]
}

this method works fine for classes with one paramList and how to do instantiate class with ()() in primary constructor? I suppose my instantiate method would have signature:

instantiate[T](aClass: Class[T], args: List[List[Any]]): T = ???

thanx


Solution

  • Solved! It's very simple. Just image that there is only one param list with summ of parameters class A(x, y) instead of A(x)(y)