"eval" in Scala

Thilo picture Thilo · Jul 26, 2009 · Viewed 26.8k times · Source

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.

Answer

ninjagecko picture ninjagecko · May 21, 2011

it's now 2011, and you can do so with scala.tools.nsc.Interpreter

see http://blog.darevay.com/2009/01/remedial-scala-interpreting-scala-from-scala/