Scala - 初始化REPL环境

6
-嗨。我想在我的应用程序中嵌入具有初始化环境的Scala REPL。我查看了IMain类,似乎可以通过其实例来执行此操作。该实例是通过ILoop的process()中的intp公共var创建并存储的。
-在process()(例如,在REPL之前)之前如何绑定某些名称和/或添加某些导入?
-以下代码在第3行失败,因为尚未创建intp(=> NPE):
    val x = 3
    val interp = new ILoop
    interp.bind("x", x) // -> interp.intp.bind("x", x)
    val settings = new Settings
    settings.usejavacp.value = true
    interp.process(settings)

感谢-。
更新:覆盖createInterpreter()不幸地没有起作用:
    val x = 3
    val interp = new ILoop {
        override def createInterpreter() {
            super.createInterpreter()
            intp.bind("x", x) // -> interp.intp.bind("x", x)
        }
    }
    val settings = new Settings
    settings.usejavacp.value = true
    interp.process(settings)

解释器在输入上卡住了(看起来像死锁,只发生在以上的代码中):

x: Int = 3
Failed to created JLineReader: java.lang.NoClassDefFoundError: scala/tools/jline/console/completer/Completer
Falling back to SimpleReader.
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.7.0_06-icedtea).
Type in expressions to have them evaluated.
Type :help for more information.

scala> println
<infinite_sleep>

感谢dvigal的建议。

2个回答

4

有一个名为 scala-ssh-shell 的GitHub项目,可能可以实现你想要的功能,或者至少让你更接近目标。


1
-嗨,抱歉我不是Scala REPL黑客,但我认为你可以做类似的事情:
class YourILoop(in0: Option[BufferedReader], protected override val out: JPrintWriter)         
    extends ILoop(in0, out) {
    override def createInterpreter() {
       if (addedClasspath != "")
          settings.classpath append addedClasspath

          intp = new ILoopInterpreter
          val x = 3;
          intp.bind("x", x)
    }
}
object Run {
    def errorFn(str: String): Boolean = {
      Console.err println str
      false
    }

    def process(args: Array[String]): Boolean = {
        val command = new GenericRunnerCommand(args.toList, (x: String) => errorFn(x))
        import command.{ settings, howToRun, thingToRun }
        new YourILoop process settings
    }
    def main(args: Array[String]) {
        process(args)  
    }
}

不错,谢谢。很遗憾,它不起作用。我已经更新了答案。 - woky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接