I want my Scala code to take a Scala class as input, compile and execute that class. How can I programmatically invoke a Scala compiler? I will be using the latest Scala version, i.e. 2.10.
相关问题
- Unusual use of the new keyword
- Get Runtime Type picked by implicit evidence
- What's the point of nonfinal singleton objects
- PlayFramework: how to transform each element of a
- Error in Scala Compiler: java.lang.AssertionError:
相关文章
- Gatling拓展插件开发,check(bodyString.saveAs("key"))怎么实现
- RDF libraries for Scala [closed]
- Why is my Dispatching on Actors scaled down in Akk
- How do you run cucumber with Scala 2.11 and sbt 0.
- GRPC: make high-throughput client in Java/Scala
- Setting up multiple test folders in a SBT project
- Testing request with CSRF Token in Play framework
- Run project with java options via sbt
The class to be compiled and run (in file test.scala)
// compileAndRun.scala (in same directory)
ToolBox
I think the proper way of invoking the Scala compiler is doing it via Reflection API documented in Overview. Specifically, Tree Creation via parse on ToolBoxes section in 'Symbols, Trees, and Types' talks about parsing
String
intoTree
usingToolBox
. You can then invokeeval()
etc.scala.tools.nsc.Global
But as Shyamendra Solanki wrote, in reality you can drive scalac's
Global
to get more done. I've written CompilerMatcher so I can compile generated code with sample code to do integration tests for example.scala.tools.ncs.IMain
You can invoke the REPL
IMain
to evaluate the code (this is also available in the aboveCompilerMatcher
if you want something that works with Scala 2.10):This was demonstrated in Embedding the Scala Interpreter post by Josh Suereth back in 2009.