views:

1832

answers:

3
+7  Q: 

"eval" in Scala

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.

+1  A: 

Well, scala does not officialy support jsr 223 (scritping languages jsr), but some people have tried their best:

http://code.google.com/p/netgents/downloads/list

More info on official scalas's trac thread: http://lampsvn.epfl.ch/trac/scala/ticket/874

Daniel Ribeiro
Another place to look at is Lord of the REPLS, which has a read-eval-print-loop for Scala, Ruby, Python, Scheme, JavaScript, Groovy, Clojure and BeanShell running on Google App Engine: http://lotrepls.appspot.com
Thilo
+15  A: 

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.

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.

Daniel Spiewak
+1 Scala does support being run from a text file like a scripting language, but it isn't a scripting language. It sounds like the author is interested in providing a scripting interface to his app, which JavaScript, Groovy or Lua would be better suited for.
Kevin Peterson
Okay, I am going to grudgingly accept this answer. Too bad, since the Scala docs seem to highlight their interpreter as a significant feature. One would think that this would be using some kind of "eval" under the hood. Apparently not.
Thilo
The interpreter is not a language feature, it's just part of the tool set. Look at Haskell as a good example. GHC Haskell provides the `ghc` tool, which is the compiler, and `ghci`, which is the interactive shell. It's an "interpreter" just like Scala's REPL, but there is really no way to use it within a Haskell function. As mentioned previously, to allow this would be horribly unsafe (type-wise) and not in line with the language as a whole.
Daniel Spiewak
+1  A: 

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.

Kim
Well, that would work. The "interpreter" invokes the compiler internally anyway. But it seems that the compiler is not any more straightforward to embed than the interpreter is.
Thilo
Depends on how you want to "embed" it. The easiest way is to call it as an external program. But if you want better integration of your program with the compiler, you can find a discussion of how to do that here: http://www.nabble.com/Compiler-API-td12050645.html
Kim