Created
September 19, 2012 01:17
-
-
Save x3ro/3747057 to your computer and use it in GitHub Desktop.
Scala 2.10 – Reflection against Named Arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.reflect.runtime.universe._ | |
import scala.reflect.runtime._ | |
def newInstance(tpe:Type, args:Map[String, Any]) = { | |
val clazz = tpe.typeSymbol.asClass | |
val ctor = tpe.member(nme.CONSTRUCTOR).asMethod | |
var argList = ctor.params(0).map { a => args(a.name.toString) } | |
currentMirror.reflectClass(clazz).reflectConstructor(ctor)(argList:_*) | |
} | |
println(newInstance(typeOf[Foo], args)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment