https://kotlinlang.org logo
#announcements
Title
# announcements
h

herder

08/08/2017, 6:13 AM
I just played around with the
kotlinc
REPL, and pretty quickly got a crash while doing this:
Copy code
>>> data class Node<T>(var prev: Node<T>?, var next: Node<T>?, val data: T)
>>> val first = Node(null, null, "foo")
>>> val next = Node(first, null, "bar")
>>> first.next = next
>>> first
exception: java.lang.StackOverflowError
	at Line_2$Node.toString(Line_2.kts)
	at java.lang.String.valueOf(String.java:2994)
	at java.lang.StringBuilder.append(StringBuilder.java:131)
Is there some way to make the built-in
toString
detect these kinds of loops, or do I need to create my own for these kinds of structures?
k

karelpeeters

08/08/2017, 7:14 AM
You'll have to roll your own.