此文件頁面專門針對 Scala 2 中發布的功能,這些功能已在 Scala 3 中移除或由其他功能取代。除非另有說明,此頁面中的所有程式碼範例都假設您使用的是 Scala 2。
REPL 可以嵌入並以程式化方式呼叫。
它支援 javax.script
API,或可用作程式碼直譯器或互動式命令列。
import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter._
import javax.script._
/** A simple example showing programmatic usage of the REPL. */
object Main extends App {
// the REPL has some support for javax.script
val scripter = new ScriptEngineManager().getEngineByName("scala")
scripter.eval("""println("hello, world")""")
// compiler settings
val settings = new Settings
settings.processArgumentString("-deprecation -feature -Xfatal-warnings -Xlint")
// the interpreter is used by the javax.script engine
val intp = new IMain(settings)
def interpret(code: String): Unit = {
import Results._
val res = intp.interpret(code) match {
case Success => "OK!"
case _ => "Sorry, try again."
}
println(res)
}
interpret("""println("hello, world")""")
interpret("""println(""")
interpret("""val who = "world" ; println("hello, $who")""")
// the REPL uses a line reader and an interpreter interactively
val interactive = new ILoop()
interactive.process(settings)
// input to the REPL can be provided programmatically
import java.io.{BufferedReader, StringReader, PrintWriter}
val reader = new BufferedReader(new StringReader(""""hello, world""""))
val canned = new ILoop(reader, new PrintWriter(Console.out, true))
canned.process(settings)
// more canning
val code = """println("hello, world") ; 42"""
val out = ILoop.run(code)
println(s"Output is $out")
}