vendredi 21 janvier 2022

[Scala Toolbox]: Compile a Case Class at run-time and then instantiate it

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("test@gmail.com")"""
val tree = toolbox.parse(codeToCompile)
val binary = toolbox.compile(tree)()

I get the error: Authentication not found ... How can I do it ?





Aucun commentaire:

Enregistrer un commentaire