Question

The scala.tools.nsc API has been completely changed from 2.10 to 2.11. For example, there's no Interpreter anymore.

I'm trying to build a site similar to http://codingbat.com in Scala, so I need the ability to compile and run code that's provided by a user while my program is running.

Does anyone know of something that explains how to use the new API anywhere? (I didn't have any luck Googling what I thought were reasonable search terms.) If not, could someone who knows something about the new API provide a small working example that lets you compile code and then retrieve the result of running it?

UPDATE 2.11.0-M5 seems to break Script Engine support, but I went back to 2.11.0-M4 and I'm able to use ScriptEngineManager to get an interpreter. The weird thing is, the type is scala.tools.nsc.interpreter.IMain. That package and class don't appear in the 2.11.0-M4 Scaladocs, so I'm wondering if they've moved out of the compiler artifact somewhere else, or if there's just an oversight in Scaladoc production and they should still be there.

Was it helpful?

Solution

Scala 2.11.0-M4 now has JSR 223 scripting support which, I think, might supersede some of the functionality you are looking for. See docs.scala-lang.org/scala/2.11

The feature is broken in scala 2.11.0-M5 but apparently fixed in 2.11.0-M6 and 2.11.0-M4

 Welcome to Scala version 2.11.0-M4 (OpenJDK Server VM, Java 1.7.0_25).
 Type in expressions to have them evaluated.
 Type :help for more information.

 scala> import javax.script.ScriptEngineManager
 import javax.script.ScriptEngineManager

 scala> val e = new ScriptEngineManager().getEngineByName("scala")
 e: javax.script.ScriptEngine = scala.tools.nsc.interpreter.IMain@1741b6d

See the ScriptEngine Javadoc and the javax.script package summary

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top