在Scala3中如何在运行时编译和执行Scala代码?

c0vxltue  于 2022-10-01  发布在  Scala
关注(0)|答案(0)|浏览(85)

我想使用Scala3编译和执行在运行时以字符串形式给出的Scala代码。例如,在Scala2中,我会使用反射

import scala.reflect.runtime.universe as ru
import scala.tools.reflect.ToolBox
val scalaCode = q"""println("Hello world!")"""
val evalMirror = ru.runtimeMirror(this.getClass.getClassLoader)
val toolBox = evalMirror.mkToolBox()
toolBox.eval(scalaCode) //Hello world!

如果我尝试在Scala3中运行这段代码,我会得到

Scala 2 macro cannot be used in Dotty. See https://dotty.epfl.ch/docs/reference/dropped-features/macros.html
To turn this error into a warning, pass -Xignore-scala2-macros to the compiler

我如何在Scala3中翻译这段代码?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题