Can Scala be used to script a Java application?
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.
The Scala documentation shows how easy it is to call compiled Scala code from Java (because it gets turned into to regular JVM bytecode).
But how can I evaluate a Scala expression on the fly (from Java or if that is easier, from within 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.
Source: Tips4all
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.
ReplyDeleteMy 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.
it's now 2011, and you can do so with scala.tools.nsc.Interpreter
ReplyDeletesee http://blog.darevay.com/2009/01/remedial-scala-interpreting-scala-from-scala/
You can emulate "eval" by taking scala code, wrapping it in a class, compiling that class, using reflection to create a new instance, and then calling it. It's a little involved, and the scala compiler is very slow (on the order of 2 seconds) to initialize, but it works fine.
ReplyDeleteThere's a library for it here, called "util-eval": https://github.com/twitter/util/
The code in question lives here: https://github.com/twitter/util/blob/master/util-eval/src/main/scala/com/twitter/util/Eval.scala
It works like this:
val sum = Eval[Int]("1 + 1")
// sum will be 2
Well, scala does not officialy support jsr 223 (scritping languages jsr), but some people have tried their best:
ReplyDeletehttp://code.google.com/p/netgents/downloads/list
More info on official scalas's trac thread: http://lampsvn.epfl.ch/trac/scala/ticket/874
You can always use scalac to compile a scala class and then load that class dynamically. But I guess that's not what you're after.
ReplyDelete