" EVAL"在斯卡拉 [英] "eval" in Scala

查看:133
本文介绍了" EVAL"在斯卡拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用Scala编写Java应用程序脚本吗?

Can Scala be used to script a Java application?

我需要从Java加载一段Scala代码,为它设置执行范围(数据暴露)由主机应用程序),评估它并从中检索结果对象。

I need to load a piece of Scala code from Java, set up an execution scope for it (data exposed by the host application), evaluate it and retrieve a result object from it.

Scala文档显示了从Java调用编译的Scala代码是多么容易(因为它变成了常规的JVM字节码)。

The Scala documentation shows how easy it is to call compiled Scala code from Java (because it gets turned into to regular JVM bytecode).

但是如何动态评估Scala表达式(从Java或者如果在Scala中更容易)?

But how can I evaluate a Scala expression on the fly (from Java or if that is easier, from within Scala) ?

对于许多其他语言,有javax.scripting接口。 Scala似乎不支持它,我在Java / Scala互操作性文档中找不到任何不依赖于提前编译的东西。

For many other languages, there is the javax.scripting interface. Scala does not seem to support it, and I could not find anything in the Java/Scala interoperability docs that does not rely on ahead-of-time compilation.

推荐答案

Scala不是一种脚本语言。它可能看起来像某种脚本语言,人们可能会为此目的提倡它,但它并不适合JSR 223脚本框架(面向动态类型语言)。要回答您的原始问题,Scala没有 eval 函数,就像Java没有 eval 一样。鉴于这些语言本质上是静态的,这样的函数对这两种语言都没有意义。

Scala is not a scripting language. It may look somewhat like a scripting language, and people may advocate it for that purpose, but it doesn't really fit well within the JSR 223 scripting framework (which is oriented toward dynamically typed languages). To answer your original question, Scala does not have an eval function just like Java does not have an eval. Such a function wouldn't really make sense for either of these languages given their intrinsically static nature.

我的建议:重新考虑你的代码,这样你就不需要 eval (你很少这样做,即使是拥有它的语言,比如Ruby)。或者,也许您根本不想在应用程序的这一部分使用Scala。如果你真的需要 eval ,请尝试使用JRuby。 JRuby,Scala和Java很好地融合在一起。使用Java部分系统很容易,部分使用Scala,另一部分(需要 eval 的部分)在Ruby中。

My advice: rethink your code so that you don't need eval (you rarely do, even in languages which have it, like Ruby). Alternatively, maybe you don't want to be using Scala at all for this part of your application. If you really need eval, try using JRuby. JRuby, Scala and Java mesh very nicely together. It's quite easy to have part of your system in Java, part in Scala and another part (the bit which requires eval) in Ruby.

这篇关于" EVAL"在斯卡拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆