Lar*_*ken 82 debugging interpreter scala
我来自Python背景,我可以添加代码中的任何位置
import pdb; pdb.set_trace()
Run Code Online (Sandbox Code Playgroud)
并且在运行时我将被放入该位置的交互式解释器中.是否有scala的等价物,或者这在运行时是不可能的?
Dan*_*ral 77
是的,你可以,在Scala 2.8上.请注意,为此,您必须在类路径中包含scala-compiler.jar.如果您使用scala程序调用scala
它,它将自动完成(或者在我所做的测试中似乎如此).
然后你可以像这样使用它:
import scala.tools.nsc.Interpreter._
object TestDebugger {
def main(args: Array[String]) {
0 to 10 foreach { i =>
breakIf(i == 5, DebugParam("i", i))
println(i)
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以传递多个DebugParam
参数.当REPL出现时,右侧的值将绑定到您在左侧提供的名称的val.例如,如果我改变这样的行:
breakIf(i == 5, DebugParam("j", i))
Run Code Online (Sandbox Code Playgroud)
然后执行将发生如下:
C:\Users\Daniel\Documents\Scala\Programas>scala TestDebugger
0
1
2
3
4
j: Int
scala> j
res0: Int = 5
Run Code Online (Sandbox Code Playgroud)
您可以通过键入继续执行:quit
.
您也可以无条件地通过调用拖放到REPL break
,其接收List
的DebugParam
,而不是一个可变参数.这是一个完整的示例,代码和执行:
import scala.tools.nsc.Interpreter._
object TestDebugger {
def main(args: Array[String]) {
0 to 10 foreach { i =>
breakIf(i == 5, DebugParam("j", i))
println(i)
if (i == 7) break(Nil)
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
C:\Users\Daniel\Documents\Scala\Programas>scalac TestDebugger.scala
C:\Users\Daniel\Documents\Scala\Programas>scala TestDebugger
0
1
2
3
4
j: Int
scala> j
res0: Int = 5
scala> :quit
5
6
7
scala> j
<console>:5: error: not found: value j
j
^
scala> :quit
8
9
10
C:\Users\Daniel\Documents\Scala\Programas>
Run Code Online (Sandbox Code Playgroud)
Kip*_*ros 24
要添加到Daniel的答案,从Scala 2.9开始,其中包含break
和breakIf
方法scala.tools.nsc.interpreter.ILoop
.此外,DebugParam
现在NamedParam
.
Răz*_*nda 24
IntelliJ IDEA:
Evaluate Expression
(Alt+ F8,在菜单中:运行 - >评估表达式)窗口以运行任意Scala代码.日食:
作为斯卡拉2.10两者break
并breakIf
已经从删除ILoop
.
要闯入翻译,你必须ILoop
直接工作.
首先添加scala compiler
库.对于Eclipse Scala,右键单击project => Build Path
=> Add Libraries...
=> Scala Compiler
.
然后你可以使用以下你想要启动解释器的地方:
import scala.tools.nsc.interpreter.ILoop
import scala.tools.nsc.interpreter.SimpleReader
import scala.tools.nsc.Settings
val repl = new ILoop
repl.settings = new Settings
repl.settings.Yreplsync.value = true
repl.in = SimpleReader()
repl.createInterpreter()
// bind any local variables that you want to have access to
repl.intp.bind("row", "Int", row)
repl.intp.bind("col", "Int", col)
// start the interpreter and then close it after you :quit
repl.loop()
repl.closeInterpreter()
Run Code Online (Sandbox Code Playgroud)
在Eclipse Scala中,可以从Console
视图中使用解释器: