kotlinx.collections.immutable
kotlinx.collections.immutable copied to clipboard
Make ordered & non-ordered set/map implementations distinguishable
I am adding some API on top of the provided persistent types, but I am running into trouble with maps/sets, because ordered and non-ordered implementations are indistinguishable. My specific use case is making the types @Stable
for Jetpack Compose, but I believe this issue applies generally to extending the existing APIs.
For example:
@Stable
interface StablePersistentList<out E> : PersistentList<E>
val <E> PersistentList<E>.stable: StablePersistentList<E>
get() = if (isEmpty()) EmptyStableList
else object : StablePersistentList<E>, PersistentList<E> by this {}
private val EmptyStableList: StablePersistentList<Nothing> =
object : StablePersistentList<Nothing>, PersistentList<Nothing> by persistentListOf() {}
This works well, but in the case of sets & maps I cannot similarly defer to an EmptyStableSet/EmptyStableMap, because there are 2 distinct implementations (ordered/non-ordered), and there is no way to determine which one is passed in.
Ralated https://github.com/Kotlin/kotlinx.collections.immutable/issues/106