【问题标题】:How to write Scala 2.9 code that will allow dropping into an interpreter如何编写允许放入解释器的 Scala 2.9 代码
【发布时间】:2012-01-18 20:08:59
【问题描述】:

我不确定如何编写允许将解释器放入 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

【问题讨论】:

  • 今晚我也遇到了这个问题。甚至:object Main extends App { scala.tools.nsc.interpreter.ILoop.breakIf(true) } 将挂起。

标签: debugging scala interpreter


【解决方案1】:

所以我知道这是一个老问题,但是如果您的 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()

【讨论】:

  • 这个解决方案确实为我解决了 OP 遇到的问题。您甚至可以通过这种方式编写自己的breakbreakIf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2015-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2017-09-06
相关资源
最近更新 更多