Hi there :slightly_smiling_face: I found an Issue ...
# stdlib
a
Hi there 🙂 I found an Issue on YouTrack closed in 2018, where there was a discussion to include the Collection utils functionalities into the standard lib: https://youtrack.jetbrains.com/issue/KT-2460/Provide-java.util.Collections-methods-as-extensions-for-collections I needed for a Project the
swap
and the and the
toSynchonized
from the Java utils. The
swap
I created smaller utilities to be able to swap an element within a
MutableList
and to retrieve a copy of a
List
with the swapped elements. I had the idea to crate a PR to provide
swap
functionalities in Kotlin, what do you think? I also discovered that you can swap elements within a normal
List
with the Collection utils. Is that a Bug?
Copy code
import java.util.Collections

val list = listOf("A", "B", "C")

println(list) // [A, B, C]
Collections.swap(list, 0, 2)
println(list) // [C, B, A]
k
Looks like an existing feature request: https://youtrack.jetbrains.com/issue/KT-33768/Add-Collection.swap-extension-to-stdlib I think what you discovered and asked if it's a bug, is something unavoidable. I can't think of a way to prevent it.
a
Thank you 🙂 Yes there it it also denied for getting into the core. Is it so uncommon to use a swap? What would be an alternative? The only way to prevent that, I could think of, would be an unmodifiable List, that prevents to use
set
. But I don't think that would work reliable.