debugging - 如何编写允许放入解释器的 Scala 2.9 代码

标签 debugging scala interpreter

我不确定如何编写允许将解释器插入 Scala 2.9 代码的代码。这个问题是this one的后续问题,询问 Scala 的等价物是什么,

import pdb
pdb.set_trace()

来自Python。那里给出的建议主要针对 Scala 2.8,相关的包不再以以前的形式存在。即,

  1. scala.nsc.tools.nsc.Interpreter.{break、breakIf} 已移至 scala.nsc.tools.nsc.interpreter.ILoop.{break、breakIf}
  2. DebugParam 现在是 scala.tools.nsc.interpreter 中的 NamedParam

正如原帖中所述,父进程的类路径不会自动传递给新解释器,因此提出了一个解决方法 here 。不幸的是,那里调用的许多类/方法现在已经改变,我不太确定如何修改代码以使其行为符合“预期”。

谢谢!

编辑:这是我的测试代码,当前可以编译并运行,但是如果由 scalac 编译并由 scala< 执行,则尝试在调试器中执行任何内容都会导致应用程序卡住

import scala.tools.nsc.interpreter.ILoop._

object Main extends App {

  case class C(a: Int, b: Double, c: String) {
    def throwAFit(): Unit = {
      println("But I don't wanna!!!")
    }
  }

  // main
  override def main(args: Array[String]): Unit = {

    val c = C(1, 2.0, "davis")

    0.until(10).foreach {
      i => 
        println("i = " + i)
        breakIf(i == 5)
    }
  }
}

EDIT2:由于我当前的设置是通过 sbt 运行的,我发现这个主题已涵盖 in the FAQ (页面底部)。但是,我不明白给出的解释,对 MyType 的任何澄清都是非常宝贵的。

EDIT3:关于该主题的另一次讨论但没有解决方案:http://permalink.gmane.org/gmane.comp.lang.scala.simple-build-tool/1622

最佳答案

所以我知道这是一个老问题,但是如果你的 REPL 挂起,我想知道问题是否在于 you need to supply the -Yrepl-sync option ?当我的嵌入式 REPL 遇到类似情况时,这为我解决了问题。

要在嵌入式 REPL 中设置 -Yrepl-sync,而不是使用 breakIf,您需要 work with the ILoop directly这样您就可以访问 Settings 对象:

// create the ILoop
val repl = new ILoop
repl.settings = new Settings
repl.in = SimpleReader()

// set the "-Yrepl-sync" option
repl.settings.Yreplsync.value = true

// start the interpreter and then close it after you :quit
repl.createInterpreter()
repl.loop()
repl.closeInterpreter()

关于debugging - 如何编写允许放入解释器的 Scala 2.9 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8916699/

相关文章:

ruby - ruby代码是如何执行的

c - GDB 单步执行汇编并显示下一条将要执行的指令。

php - 在 Codeception 中将调试输出打印到控制台

Android Studio 调试和 DDMS

scala - 我该如何管理案例类的构造函数?

scala - Scala 编译器如何进行隐式转换?

debugging - 在 GHCi 调试器中中断并继续,而不使用断点

scala - 如何在Spark SQL(DataFrame)的UDF中使用常量值

Haskell:不同语言的实际 IO monad 实现?

c - 构建解释器 : designing an AST