gpt4 book ai didi

scala - 从字符串生成一个类并在 Scala 2.10 中实例化它

转载 作者:行者123 更新时间:2023-12-02 09:38:28 26 4
gpt4 key购买 nike

在 Scala 2.10 中,如何从字符串(可能使用 Toolbox api)生成一个类,然后用 Scala 的反射进行实例化?

最佳答案

W.r.t 编译工具箱只能运行表达式 = 返回值,但不能运行带有编译结果的类或文件/字节数组。

但是,仍然可以实现您想要的,因为在 Scala 中,使用隐式值从类型级别到值级别非常容易:

编辑 .在 2.10.0-RC1 中 ToolBox 的一些方法已更名。 parseExpr现在只是 parse , 和 runExpr现在称为 eval .

scala> import scala.reflect.runtime._ // requires scala-reflect.jar
// in REPL it's implicitly added
// to the classpath
// but in your programs
// you need to do this on your own
import scala.reflect.runtime

scala> val cm = universe.runtimeMirror(getClass.getClassLoader)
cm @ 41d0fe80: reflect.runtime.universe.Mirror = JavaMirror with scala.tools.nsc.interpreter.IMain$TranslatingClassLoader...

scala> import scala.tools.reflect.ToolBox // requires scala-compiler.jar
// in REPL it's implicitly added
// to the classpath
// but in your programs
// you need to do this on your own
import scala.tools.reflect.ToolBox

scala> val tb = cm.mkToolBox()
tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] = scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@3a962da5

scala> tb.runExpr(tb.parseExpr("class C; scala.reflect.classTag[C].runtimeClass"))
res2: Any = class __wrapper$1$f9d572ca0d884bca9333e251c64e980d$C$1

更新#1。如果不需要java.lang.Class,只需要实例化编译好的类,可以写 new C直接在字符串中提交 runExpr .

更新#2。也可以有 runExpr使用从变量名到运行时值的自定义映射。例如:
scala> val build = scala.reflect.runtime.universe.build
build: reflect.runtime.universe.BuildApi = scala.reflect.internal.BuildUtils$BuildImpl@50d5afff

scala> val x = build.setTypeSignature(build.newFreeTerm("x", 2), typeOf[Int])
x: reflect.runtime.universe.FreeTermSymbol = free term x

scala> tb.runExpr(Apply(Select(Ident(x), newTermName("$plus")), List(Literal(Constant(2)))))
res0: Any = 4

在此示例中,我创建了一个值为 2 的自由术语(该值不必是原始值 - 它可以是您的自定义对象)并将标识符绑定(bind)到它。然后在由工具箱编译和运行的代码中按原样使用该值。

该示例使用手动 AST 组装,但可以编写一个函数来解析字符串,找出未绑定(bind)的标识符,在某些映射中查找它们的值,然后创建相应的自由术语。不过 Scala 2.10.0 中没有这样的功能。

关于scala - 从字符串生成一个类并在 Scala 2.10 中实例化它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12122939/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com