Wonder why swap is not in the stdlib
# stdlib
e
Wonder why swap is not in the stdlib
j
Could you please elaborate on the signature of the function and what it would do?
e
swap(index1, index2)
move item from index1 position to index2 and item from index2 to index1
Ah oke, I was particularly thinking about swapping item in the ordered collection
Oke, looks like not widely use case
So anyone who needs - will just write three lines of code function
j
Ah I see. Thanks for clarifying. I don't know why it's not in there, but usually it means there is not a compelling reason to add it (the question is usually "why" rather than "why not"). Mutable collections are less common in general kotlin code than read-only collections, so that might be one part of the reason
e
especially Kotlin promoting immutable data types
👍 1
i
Sometimes swap is useful, mostly in advent-of-code type of tasks, but other than that we struggle to find compelling use cases. If you know some, drop a comment to https://youtrack.jetbrains.com/issue/KT-33768/Add-Collection.swap-extension-to-stdlib
d
I'd like the ability to write
swap(::a, ::b)
that swaps two `var`s, written using
KProperty
but compiled to completely eliminate that overhead and become just the standard temp-variable implementation.