在任意scala代码位置期间进入解释器

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,其接收ListDebugParam,而不是一个可变参数.这是一个完整的示例,代码和执行:

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)

  • 它是模块的依赖项,因此也是类路径中的依赖项.2.8没有将主机进程的`java -classpath`的内容传递给scalac的设置:http://old.nabble.com/-scala--recent-changes-in-2.8-nightly-classpath-management -td26233977.html (4认同)
  • 这可能导致在Scala 2.8中出现错误`scala.tools.nsc.MissingRequirementError:object scala not found.您可能需要将主机进程的类路径显式传递给Scalac的Settings,但是`break`和`breakIf`不会这样做.这是一个修补版本的"break":http://gist.github.com/290632 (3认同)

Kip*_*ros 24

要添加到Daniel的答案,从Scala 2.9开始,其中包含breakbreakIf方法scala.tools.nsc.interpreter.ILoop.此外,DebugParam现在NamedParam.

  • 你能用新用法写一个例子吗? (8认同)

Răz*_*nda 24

IntelliJ IDEA:

  1. 在调试模式下运行或附加远程调试器
  2. 设置断点并运行直至到达它
  3. 打开Evaluate Expression(Alt+ F8,在菜单中:运行 - >评估表达式)窗口以运行任意Scala代码.
  4. 键入要运行的代码片段或表达式,然后单击"评估"
  5. 键入Alt+ V或单击"评估"以运行代码片段.

日食:

作为斯卡拉2.10两者breakbreakIf已经从删除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视图中使用解释器:

  • 那太糟了.:( (18认同)
  • 因为它增加了许多与程序中某个点的调试目标完全无关的锅炉板,而是与获取REPL的机制有关. (13认同)
  • 完全备份丹尼尔的观点:( (4认同)