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
Mainobject:
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
RecursiveTraversablebut debugger doesn't stop at this point. I can see stdout counting and
thisat debug panel is "Collecting data...".
Any thoughts on what am I doing wrong and how can I debug this kind of code?
Please sign in to leave a comment.
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
Seems to work great. Thanks!
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!.
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?
Sure - a complete description of my issue is at https://github.com/milessabin/shapeless/issues/656