I have a question regarding data classes in Kotlin.
Say we have two classes with a circular reference:
data class Foo(
var bar: Bar? = null
)
data class Bar (
var foo: Foo? = null
)
and the main function:
fun main() {
val f = Foo()
val b = Bar()
f.bar = b
b.foo = f
f.hashCode()
b.hashCode()
//println(f)
//println(b)
}
the code that Kotlin auto generates will make the main function go in stack overflow. You can try this in Kotlin Playground.
Is this an expected behaviour? Note that this is a basic example, but this will happen in real life situation where, for example, instead of a reference of Foo in Bar we can have a List, a Set or a generic collection of Foo.
Shouldn't Kotlin generate some sort of compiler error or this happens even in other languages?
same thing but in Java