【问题标题】:how to call scala REPL console from scala program?如何从 scala 程序调用 scala REPL 控制台?
【发布时间】:2017-03-02 12:54:53
【问题描述】:

我需要在我的 Scala 应用程序中嵌入 Scala REPL 功能。我怎样才能做到这一点 ?我需要调用哪个 Scala 方法/类?

我查看了 scala.tools.nsc.interpreter 包,但我不知道该怎么做。

【问题讨论】:

  • ILoop 是 REPL,process(settings) 是入口点。 IMain 本身就是解释器,interpret(line) 是入口点。在 2.12 中还有单独的 javax.script 用法、Scripted。 Ammonite REPL 可能会提供更好的嵌入体验。

标签: java scala read-eval-print-loop scala-reflect


【解决方案1】:

@som-snytt 给了我一个提示,我得到了解决方案。

import scala.tools.nsc.interpreter._

val lines: List[String] = List("println(\"Hello\")", "println(\"Word\")")
val ret: String = ILoop.run(lines)
// Verify if all runs OK

// Read stdin
val condition = ...
while (condition) {
  // convert to List[String] 
  val ret: String = ILoop.run(lines)
  // format and print the output
  println(ret)
  // Read stdin again
}

【讨论】:

    猜你喜欢
    • 2019-04-26
    • 2017-04-23
    • 2015-05-28
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 2018-09-22
    • 2014-04-16
    • 1970-01-01
    相关资源
    最近更新 更多