Debugging scala code with recursive function

I have a trivial class named

RecursiveTraversable
:
class RecursiveTraversable extends Traversable[Any]{
  override def foreach[U](f: (Any) => U): Unit = {
    recursivePrint(1)
  }

  @tailrec
  private def recursivePrint(counter: Long): Unit = {
    println(s"Test $counter...")
    recursivePrint(counter+1)
  }
}

and a trivial

Main
object:
object Main {
  def main (args: Array[String]) {
    new RecursiveTraversable().foreach(_ => None)
  }
}

To debug this code I've set a breakpoint at

println(s"Test $counter...")
in
RecursiveTraversable
but debugger doesn't stop at this point. I can see stdout counting and
this
at debug panel is "Collecting data...".

Any thoughts on what am I doing wrong and how can I debug this kind of code?
5 comments
Comment actions Permalink

Hi, Oleg,

Intellij IDEA debugger uses toString() method to show objects in Variables View. In your case toString() method is defined in collection library using foreach method. When you stop at breakpoint, this.toString() method is called and infinite loop of recursivePrint starts.

You can either explicitly override toString method in your class or disable toString() view of objects in debugger (see attached screenshot).



Attachment(s):
toString_object_view.jpg
0
Comment actions Permalink

Seems to work great. Thanks!

0
Comment actions Permalink

I ran into this issue recently ... took quite a lot of effort to realize the cause, though it is somewhat obvious looking back on it. I didn't find this thread until afterwards. If this caveat could be mentioned somewhere more prominently, either as a warning when OOMs start popping up in the Debugger in IntelliJ/Scala, or in the docs, that would be great!.

0
Comment actions Permalink

I checked original example in the IDEA 2016.3, and it stops at the breakpoint and shows error message in Variables View:

Can you give more details about your problem?

0

Please sign in to leave a comment.