Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Scala 2.9 code that will allow dropping into an interpreter

I am not sure how to write code that will allow dropping into an interpreter into Scala 2.9 code. This question is a follow-up to this one, which asked what the Scala equivalent of,

import pdb
pdb.set_trace()

was from Python. The advice given there was primarily for Scala 2.8, and the related packages no longer exist in their previous form. Namely,

  1. scala.nsc.tools.nsc.Interpreter.{break, breakIf} have been moved to scala.nsc.tools.nsc.interpreter.ILoop.{break, breakIf}
  2. DebugParam is now NamedParam in scala.tools.nsc.interpreter

As noted in the original post, the class path of the parent process is not passed to the new interpreter automatically, so a workaround was presented here. Unfortunately, many of the classes/methods invoked there have now changed, and I'm not quite sure how to modify the code the behave as "expected".

Thanks!

EDIT: Here is my test code, which at current compiles and runs, but attempting to execute anything in the debugger results in the application freezing if compiled by scalac and executed by 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: As my current setup is running through sbt, I have discovered that this topic is covered in the FAQ (bottom of the page). However, I do not understand the explanation given, and any clarification on MyType would be invaluable.

EDIT3: another discussion on the topic without a solution: http://permalink.gmane.org/gmane.comp.lang.scala.simple-build-tool/1622

like image 347
duckworthd Avatar asked Jan 18 '12 20:01

duckworthd


1 Answers

So I know this is an old question, but if your REPL is hanging, I wonder if the problem is that you need to supply the -Yrepl-sync option? When my embedded REPL was hanging in a similar situation, that solved it for me.

To set -Yrepl-sync in an embedded REPL, instead of using breakIf you'll need to work with the ILoop directly so you can access the Settings object:

// 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()
like image 116
Steve Avatar answered Oct 15 '22 12:10

Steve