在运行时编译和执行 Scala 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7477589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:28:25  来源:igfitidea点击:

Compile and execute Scala code at runtime

scalainterpreterdsl

提问by Peter

Is is possible to compile and execute scala code as a string at runtime either in Scala or in Java?

是否可以在 Scala 或 Java 中在运行时将 Scala 代码作为字符串编译和执行?

My idea is to build a DSL using Scala then let Java programmers use the DSL inside Java.

我的想法是使用 Scala 构建一个 DSL,然后让 Java 程序员在 Java 中使用 DSL。

I heard that the class scala.tools.nsc.Interpreter can do something like that, but when I imported it inside my scala file, I got "object tools is not a member of package scala."

我听说类 scala.tools.nsc.Interpreter 可以做类似的事情,但是当我将它导入我的 scala 文件时,我得到“对象工具不是包 scala 的成员”。

So could anybody give me a hint?

所以有人可以给我一个提示吗?

回答by Eugene Burmako

In 2.10.0 we expose Scala reflection API, which among everything else includes a runtime compilation facility. More details can be found here: Generating a class from string and instantiating it in Scala 2.10.

在 2.10.0 中,我们公开了 Scala 反射 API,其中包括运行时编译工具。可以在此处找到更多详细信息:从字符串生成类并在 Scala 2.10 中实例化它

回答by gerferra

I recommend you twitter-util's Eval

我推荐你twitter-utilEval