斯卡拉 - 初始化REPL环境(Scala - Initialize REPL environmen

2019-08-01 00:03发布

嗨。 我想嵌入斯卡拉REPL与初始化环境到我的应用程序。 我已经看了IMain类,似乎我可以通过它的实例做。 该实例被创建,然后存储到intp公共变种在process()ILoop

我怎么能结合一些名称和/或前加一些进口的process() (REPL前EG)?

下面的代码在第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的建议。

Answer 1:

有一个叫GitHub的项目斯卡拉-SSH壳可以做你想做的,或者至少让你更接近。



Answer 2:

嗨,对不起,我不斯卡拉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)  
    }
}


文章来源: Scala - Initialize REPL environment