-嗨。我想在我的应用程序中嵌入具有初始化环境的Scala REPL。我查看了IMain类,似乎可以通过其实例来执行此操作。该实例是通过ILoop的process()中的intp公共var创建并存储的。
-在process()(例如,在REPL之前)之前如何绑定某些名称和/或添加某些导入?
-以下代码在第3行失败,因为尚未创建intp(=> NPE):
感谢-。
更新:覆盖createInterpreter()不幸地没有起作用:
-在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的建议。