Scala - 初始化REPL环境

wok*_*oky 6 embed scala bind read-eval-print-loop

嗨.我想将初始化环境中的 Scala REPL嵌入到我的应用程序中.我看过IMain课,似乎我可以通过它的实例来做.实例被创建,然后存储到intp在公共变种process()ILoop.

如何绑定某些名称和/或添加一些导入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)
Run Code Online (Sandbox Code Playgroud)

谢谢-.

更新: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)
Run Code Online (Sandbox Code Playgroud)

解释器停留在输入上(看起来像死锁,仅在上面的代码中发生):

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>
Run Code Online (Sandbox Code Playgroud)

感谢dvigal的建议.

Ami*_*ico 4

有一个名为scala-ssh-shell 的github 项目可以满足您的需求,或者至少让您更接近。