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

duc*_*thd 14 debugging interpreter scala

我不知道如何编写允许将解释器放入Scala 2.9代码的代码.这个问题是一个后续这一块,这要求斯卡拉相当于什么,

import pdb
pdb.set_trace()
Run Code Online (Sandbox Code Playgroud)

来自Python.那里给出的建议主要是针对Scala 2.8,相关的包不再存在于以前的形式中.也就是说,

  1. scala.nsc.tools.nsc.Interpreter.{break, breakIf} 已被转移到 scala.nsc.tools.nsc.interpreter.ILoop.{break, breakIf}
  2. DebugParam现在NamedParam在scala.tools.nsc.interpreter中

正如原始帖子中所述,父进程的类路径不会自动传递给新的解释器,因此这里提供一种解决方法.不幸的是,那里调用的许多类/方法现在已经改变了,我不太确定如何修改代码表现为"预期".

谢谢!

编辑:这是我的测试代码,在当前编译和运行,但尝试在调试器中执行任何操作导致应用程序冻结,如果编译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)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Ste*_*eve 3

所以我知道这是一个老问题,但如果你的 REPL 挂起,我想知道问题是否在于你需要提供-Yrepl-sync选项?当我的嵌入式 REPL 遇到类似情况时,这为我解决了问题。

-Yrepl-sync在嵌入式 REPL 中进行设置,breakIf您需要直接使用,ILoop而不是使用,以便可以访问该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()
Run Code Online (Sandbox Code Playgroud)