RFC: Conform IdentifiedArray
to Mutable/RangeReplaceableCollection
#1633
Replies: 2 comments 1 reply
-
I think this is a good change. I feel that the risk of inserting already existing While we're at extension IdentifiedArray where Element: Identifiable, ID == Element.ID {
public func mapElements<T: Identifiable>(transform: (Element) -> T)
-> IdentifiedArrayOf<T> where T.ID == ID {
IdentifiedArray<ID, T>(uncheckedUniqueElements: self.map {
let element = transform($0)
precondition(element.id == $0.id, "…")
return element
})
}
} and maybe another version not restricted to I think this kind of transform would help to map domains in TCA, which can sometimes be cumbersome when you already have an |
Beta Was this translation helpful? Give feedback.
-
We'd like to propose a couple changes to a data type that comes from one of the Composable Architecture's dependencies, and wanted to run those changes by the community to see if anyone finds any problems with them.
IdentifiedArray
(from Identified Collections) is a data type that the Composable Architecture uses to embed child features in collections of elements of parent domains. The type can be employed in any application, though, and is a great tool to reach for when working with collections in SwiftUI state that render inForEach
views. It can even be handy to reach for in UIKit.This data type is a defined as a wrapper around one of Apple's Swift Collections, in particular its
OrderedDictionary
. We leverage all of Apple's work to make the collection as efficient and performant to use as possible.One decision Apple has made with
OrderedDictionary
is to withhold direct conformances toMutableCollection
andRangeReplaceableCollection
, offering "partial" conformances instead, which simply define a subset of functionality.—https://github.com/apple/swift-collections/blob/main/Documentation/OrderedDictionary.md#sequence-and-collection-operations
We've conservatively preserved this behavior on
IdentifiedArray
: it does not currently conform toMutableCollection
orRangeReplaceableCollection
, and instead provides "partial" conformances to the underlying ordered dictionary methods.We'd like to relax this constraint.
IdentifiedArray
is not semantically an ordered dictionary, despite wrapping one under the hood. It leverages this data type for performance in accessing and modifying particular elements by their identifiers (by keying each element by its identifier), and maintains pre-/postconditions that the key and identifier are always identical. We expectIdentifiedArray
to be treated more or less as an array with preconditions, and think it should benefit from all the same algorithms thatArray
does via these conformances.For example, we really do think
IdentifiedArray
is a great data type for use in vanilla SwiftUI, but because it does not conform toMutableCollection
andRangeReplaceableCollection
, it already misses out on some great uses.For one,
MutableCollection
will allow us to pass a binding of an identified array to aForEach
view:And
RangeReplaceableCollection
will allow us to pass a binding of an identified array to aNavigationStack
:Can anyone think of any issues with supporting these conformances? Are there guarantees that
MutableCollection
andRangeReplaceableCollection
provide that we are skirting, and even if there are, is it pragmatic to skirt them with preconditions?We've opened a pull request with this functionality for those that want to take it for a spin and try to break it.
Beta Was this translation helpful? Give feedback.
All reactions