【发布时间】:2012-09-19 17:58:20
【问题描述】:
-嗨。我想将带有 初始化环境 的 Scala REPL 嵌入到我的应用程序中。我看过IMain 类,看来我可以通过它的实例来做到这一点。创建实例,然后将其存储到process() 的ILoop 中的intp public 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 的建议。
【问题讨论】:
标签: scala embed bind read-eval-print-loop