I'm trying to define a case class using Scala Reflection Toolbox and to instantiate it. What is the best way to do it?
At the moment I do
val codeToCompile = q"""case class Authentication(email:String)"""
val tree = toolbox.parse(codeToCompile)
val classDefinition : ClassDef = tree.asInstanceOf[ClassDef]
val definedClass = toolbox.define(classDefinition)
I would like to use the constructor of the case class to instantiate it at run-time after I have defined it into the Toolbox, Like
val codeToCompile = q"""val myAuth = Authentication("[email protected]")"""
val tree = toolbox.parse(codeToCompile)
val binary = toolbox.compile(tree)()
I get the error: Authentication not found ... How can I do it ?
Firstly,
sinterpolator generates strings,qinterpolator generates trees. So either useq"..."without.parseor uses"..."with.parse.Secondly, make use of
ClassSymboldefinedClass, which you have.Thirdly,
.applyis a companion-object method. So use.companion.Forthly, there is not much sense in defining a local variable in single-line
q"val x = ???"with toolbox, anyway it will be hard to use thisTermSymbollater (on contrary toClassSymbol).Try