Scala-初始化REPL环境 [英] Scala - Initialize REPL environment
问题描述
-嗨.我想将具有初始化环境的Scala REPL嵌入到我的应用中.我看过IMain
类,似乎可以通过它的实例来做到这一点.创建该实例,然后将其存储到ILoop
的process()
中的intp
公共变量中.
-Hi. I'd like to embed Scala REPL with initialized environment into my app. I've looked at IMain
class and it seems I could do it via instance of it. The instance is created and then stored into intp
public var in process()
of ILoop
.
如何在process()
之前(例如,在REPL之前)绑定一些名称和/或添加一些导入内容?
How can I bind some names and/or add some imports before process()
(e.g. before REPL)?
以下代码在第3行失败,因为尚未创建intp
(=> NPE):
Following code fails on line 3 because intp
is not yet created (=> 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()
不起作用:
UPDATE: Overriding createInterpreter()
unfortunately doesn't work:
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)
解释器卡在输入上(看起来像死锁,仅在上面的代码中发生):
Interpreter is stuck on input (looks like deadlock, happens only with code above):
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>
感谢大家的建议.
推荐答案
有一个名为的github项目. scala-ssh-shell 可能会做您想要的事情,或者至少使您离它更近.
There is a github project called scala-ssh-shell which may do what you want, or at least get you closer.
这篇关于Scala-初始化REPL环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!