我尝试使用scala.tools.nsc.interpreter
启用交互式调试(如Python的pdb / ipdb):
val foo = 123
import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.{ ILoop, SimpleReader }
val repl = new ILoop
repl.settings = new Settings
repl.settings.usejavacp.value = true
repl.in = SimpleReader()
repl.createInterpreter()
repl.intp.bind("foo", "Int", foo)
repl.loop()
repl.closeInterpreter()
运行时,这就是我得到的:
$ scala repl.scala
foo: Int = 123
scala> "hello"
java.lang.NullPointerException
at scala.concurrent.Await$$anonfun$ready$1.apply(package.scala:95)
at scala.concurrent.Await$$anonfun$ready$1.apply(package.scala:95)
at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:53)
at scala.concurrent.Await$.ready(package.scala:95)
at scala.tools.nsc.interpreter.ILoop.processLine(ILoop.scala:402)
at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:430)
at Main$$anon$1.<init>(repl.scala:14)
at Main$.main(repl.scala:1)
at Main.main(repl.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at scala.reflect.internal.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:70)
at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:101)
at scala.reflect.internal.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:70)
at scala.reflect.internal.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
at scala.tools.nsc.CommonRunner$class.run(ObjectRunner.scala:22)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:39)
at scala.tools.nsc.CommonRunner$class.runAndCatch(ObjectRunner.scala:29)
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:39)
at scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:175)
at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:192)
at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:192)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1$$anonfun$apply$mcZ$sp$1.apply(ScriptRunner.scala:161)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:161)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:129)
at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:129)
at scala.tools.nsc.util.package$.trackingThreads(package.scala:43)
at scala.tools.nsc.util.package$.waitingForThreads(package.scala:27)
at scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:128)
at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:192)
at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:205)
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:67)
at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Abandoning crashed session.
scala>
如果我删除repl.intp.bind("foo", "Int", foo)
部分,这就是我得到的:
$ scala repl.scala
scala> 123
null
Abandoning crashed session.
scala>
我做错了什么?在程序运行期间是否有更简单的方法可以放入交互式REPL以进行调试?断点,踩踏和检查当地人有时候不会这样做。
我在Scala 2.11.5上。
答案 0 :(得分:1)
我不知道为什么这条评论没有神奇的星号:
// start an interpreter with the given settings
def process(settings: Settings): Boolean
所以你可以:
scala> repl process s
Welcome to Scala version 2.11.4 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :quit
res0: Boolean = true
或者你可以:
package myrepl
import scala.tools.nsc.Settings
import scala.tools.nsc.interpreter.{ ILoop, SimpleReader }
object Test extends App {
val foo = 42
val repl = new ILoop {
override def printWelcome() = {
intp.bind("foo", foo)
super.printWelcome()
echo("Customized...")
}
}
val s = new Settings
s.usejavacp.value = true
repl.in = SimpleReader()
repl process s
/*
repl.createInterpreter()
repl.intp.bind("foo", "Int", foo)
repl.loop()
repl.closeInterpreter()
*/
}
和
$ scalac myrepl.scala && scala myrepl.Test
foo: Int = 42
Welcome to Scala version 2.11.5 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
Customized...
scala> foo
res0: Int = 42
scala> :quit
此外,
s.Xnojline.value = true
//repl.in = SimpleReader()
由于repl崩溃的错误,我将降级到2.11.4 ......
Sbt由overriding createInterpreter
and calling bind there完成。
编辑:
val repl = new ILoop {
override def printWelcome() = {
//import scala.concurrent.duration._
//Await.ready(globalFuture, 10.minutes) // sorry, it's private!
super.printWelcome()
echo("Customizing...")
processLine("") // block for init to finish
intp.bind("foo", foo)
}
}
私人globalFuture
阻碍了初创公司的爆炸:
object Solid extends App {
val foo = 42
val repl = new ILoop {
override def printWelcome() = {
super.printWelcome()
echo("Customized...")
}
}
val s = new Settings
s.Xnojline.value = true
s.usejavacp.value = true
repl.settings = s
repl.createInterpreter()
repl.in = SimpleReader()
repl.intp.initializeSynchronous()
repl.loopPostInit()
repl.globalFuture = concurrent.Future.successful(true)
repl.intp.bind("foo", "Int", foo)
try repl.loop()
finally repl.closeInterpreter()
}
2.11中不再遵守-Yrepl-sync
选项。