mercredi 26 janvier 2022

Using Scala Toolbox is it possible to define a case class and use it later into eval code?

I would like to give the case class definition to the Toolbox and be able to use them later when I do the eval. The Toolbox should be able to know the definitions of the case classes.

For example, I am able to define a case class named Authentication with args email

      import scala.reflect.runtime._
      import scala.reflect.runtime.universe._
      import scala.tools.reflect.ToolBox
      val cm = universe.runtimeMirror(getClass.getClassLoader)
      val toolBox = cm.mkToolBox()
      val myClass: ClassDef = q"case class Authentication(email: String)".asInstanceOf[ClassDef]
      val definedClass  = toolBox.define(myClass)
      println(definedClass)

Then I would like to recall it into my Eval expression and match it

  val myCode =
    q""" def myFunction(x:Any){
       x match{
        case Authentication(param) => println("Auth received!")
        }
       }"""

  toolBox.eval(myCode)

But it tells me, Authentication was not found. Any idea of how to accomplish it?





Aucun commentaire:

Enregistrer un commentaire