在任意 Scala 代码位置期间放入解释器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2160355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:53:00  来源:igfitidea点击:

Drop into interpreter during arbitrary scala code location

debuggingscalainterpreter

提问by Lars Yencken

I come from a Python background, where at any point in my code I can add

我来自 Python 背景,我可以在代码中的任何一点添加

import pdb; pdb.set_trace()

and at runtime I'll be dropped into an interactive interpreter at that spot. Is there an equivalent for scala, or is this not possible at runtime?

在运行时,我会在那个位置进入交互式解释器。scala 是否有等价物,或者这在运行时是不可能的?

采纳答案by Daniel C. Sobral

Yes, you can, on Scala 2.8. Note that, for this to work, you have to include the scala-compiler.jar in your classpath. If you invoke your scala program with scala, it will be done automatically (or so it seems in the tests I made).

是的,你可以,在 Scala 2.8 上。请注意,要使其正常工作,您必须在类路径中包含 scala-compiler.jar。如果您使用 调用您的 Scala 程序scala,它将自动完成(或者在我所做的测试中看起来如此)。

You can then use it like this:

然后你可以像这样使用它:

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)
    }
  }
}

You may pass multiple DebugParamarguments. When the REPL comes up, the value on the right will be bound to a val whose name you provided on the left. For instance, if I change that line like this:

您可以传递多个DebugParam参数。当 REPL 出现时,右侧的值将绑定到您在左侧提供的名称的 val。例如,如果我像这样更改该行:

      breakIf(i == 5, DebugParam("j", i))

Then the execution will happen like this:

然后执行将像这样发生:

C:\Users\Daniel\Documents\Scala\Programas>scala TestDebugger
0
1
2
3
4
j: Int

scala> j
res0: Int = 5

You continue the execution by typing :quit.

您可以通过键入 继续执行:quit

You may also unconditionally drop into REPL by invoking break, which receives a Listof DebugParaminstead of a vararg. Here's a full example, code and execution:

你也可以通过调用 无条件地进入 REPL break,它接收一个ListofDebugParam而不是一个 vararg。这是一个完整的示例、代码和执行:

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)
    }
  }
}

And then:

接着:

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>

回答by Kipton Barros

To add to Daniel's answer, as of Scala 2.9, the breakand breakIfmethods are contained in scala.tools.nsc.interpreter.ILoop. Also, DebugParamis now NamedParam.

为了补充丹尼尔的答案,从 Scala 2.9 开始,breakbreakIf方法包含在scala.tools.nsc.interpreter.ILoop. 还有,DebugParam现在NamedParam

回答by R?zvan Flavius Panda

IntelliJ IDEA:

智能想法:

  1. Run in debug mode or attach a remote debugger
  2. Set a breakpoint and run until you reach it
  3. Open Evaluate Expression(Alt+F8, in menu: Run -> Evaluate Expression) window to run arbitrary Scala code.
  4. Type what code fragment or expression you want to run and click on Evaluate
  5. Type Alt+Vor click on Evaluate to run the code fragment.
  1. 在调试模式下运行或附加远程调试器
  2. 设置断点并运行直到到达
  3. 打开Evaluate Expression( Alt+ F8, in menu: Run -> Evaluate Expression) 窗口运行任意 Scala 代码。
  4. 输入要运行的代码片段或表达式,然后单击 Evaluate
  5. 键入Alt+V或单击 Evaluate 以运行代码片段。

Eclipse:

蚀:

As of Scala 2.10 both breakand breakIfhave been removed from ILoop.

作为斯卡拉2.10两者breakbreakIf已经从删除ILoop

To break into interpreter you will have to work with ILoopdirectly.

要闯入口译员,您必须ILoop直接与之合作。

First add scala compilerlibrary. For Eclipse Scala, right click on project => Build Path=> Add Libraries...=> Scala Compiler.

首先添加scala compiler库。对于 Eclipse Scala,右键单击项目 => Build Path=> Add Libraries...=> Scala Compiler

And then you can use the following where you want to start the interpreter:

然后您可以在要启动解释器的位置使用以下内容:

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()

In Eclipse Scala the interpreter can be used from the Consoleview:

在 Eclipse Scala 中,可以从Console视图中使用解释器: