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

时间:2012-01-18 20:08:59

标签: 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. scila.tools.nsc.interpreter
  3. DebugParam现在是NamedParam
  4. 如原始帖子所述,父进程的类路径不会自动传递给新的解释器,因此提供了一种解决方法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

1 个答案:

答案 0 :(得分:4)

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