Kotlin / kotlinx.collections.immutable

Immutable persistent collections for Kotlin
Apache License 2.0
1.16k stars 59 forks source link

Expose Ordered types #106

Open rnett opened 3 years ago

rnett commented 3 years ago

I have a few situations where I want to enforce that my map is ordered, say for a list of settings profiles that are keyed by name, but also should have a defined order. This isn't currently possible (here or in the stdlib, although stdlib at least has LinkedHashMap) since although persistentMapOf uses PersistentOrderedMap, the type of PersistentOrderedMap isn't exposed.

This might be more appropriate as a stdlib issue, let me know if so.

qurbonzoda commented 3 years ago

Stdlib mapOf and mutableMapOf create an ordered map. Similarly, persistentMapOf returns an empty ordered map. All modification operations preserve the ordered behavior of the map. When you assign a map accepted as a function argument, the result of toPersistentMap() can be used. toPersistentMap() is no-op if the reciever is already an ordered map, and creates an ordered map with elements of the receiver otherwise.

qurbonzoda commented 3 years ago

Let's keep this issue open for further discussion.

Lordfirespeed commented 1 year ago

Any update on this? Open to contributions?

It seems like what's missing are interfaces for PersistentOrderedMap and PersistentOrderedSet, rather than exposing the implementation classes.

jpmcosta commented 11 months ago

I'm having the exact same issue. I'm not sure if the suggested solution is the best alternative, but I would find it valuable to have a way of enforcing if the preservation of order is needed or not.

Especially, since PersistentSet is described as:

A generic persistent unordered collection of elements that does not support duplicate elements, and supports adding and removing elements.

st-hocnguyen commented 2 months ago

+1 about exposing PersistentOrderedMap and PersistentOrderedSet 🙏