What is the best way to “replace” an element of an immutable list?
I came up with the following ugly solution:
val list = listOf(1, 2, 3) val updatedList = listOf(99, *(list.subList(1, list.size).toTypedArray()))
However, despite being a bit noisy it would get even worse if you want to replace an element in the middle of the list. If I’m not overlooking something, I think Kotlin needs some more methods like the
patch methods of Scala’s
My example would look like this in Scala:
val list = List(1, 2, 3) val updatedList = list.updated(0, 99)