The problem goes away when instead of data class I use simply a class or instead of a MutableSet I use an ArrayList. I am trying to understand what is wrong in the way I first wrote it and can't find anything wrong, but it throws a StackOverflow error when running the last line
data class Person(
val id : Int,
val neighbors : MutableSet<Person> = mutableSetOf()
)
fun main() {
val neighbor1 = Person(0)
val commonNeighbor = Person(1)
val neighbor2 = Person(2)
commonNeighbor.neighbors.addAll(listOf(neighbor1,neighbor2))
neighbor1.neighbors.add(commonNeighbor)
neighbor2.neighbors.add(commonNeighbor)
}