Hi everyone,
I have a new pitch ready that proposes additional API for Foundation that allows conversion between the new RangeSet type in the standard library and Foundation's IndexSet type. While IndexSet is not available in swift-foundation at the moment, I'd still appreciate any feedback that you have on this API addition! I've included a snippet of the pitch below but you can check out the full proposal here.
RangeSet/IndexSet Conversion
- Proposal: SF-NNNN
- Author(s): Jeremy Schonfeld
- Status: Pitch
- Implementation: coming soon
Introduction/Motivation
With the latest revision of SE-0270, we've landed the RangeSet type in the standard library. Foundation already has an existing type called IndexSet used in various (mainly Objective-C-based) APIs to represent a collection of integer indices. These types are fairly similar in purpose, however IndexSet is constrained to non-negative, integer indices (as required by Objective-C collections) whereas RangeSet is generic over any Comparable index type. There are some cases when developers may wish to convert a set of indices between the IndexSet and RangeSet representations. For example, library authors writing new swift APIs that wrap or interact with existing Objective-C APIs may wish to expose RangeSet-based entry points while occasionally calling to IndexSet-based Objective-C APIs as an implementation detail. Additionally, developers may wish to write apps using the new RangeSet type while still interfacing with existing SDK APIs that use IndexSet from Foundation. Foundation has the opportunity to provide these conversion initializers as a convenience to ensure both correctness and performance while moving between the two types.
Proposed solution and example
We will add new initializers in Foundation on the IndexSet and RangeSet types that allow conversion between the two when the RangeSet's Bound == Int. For examples, developers could write the following code:
extension UICollectionView {
func reloadSections(_ sections: RangeSet<Int>) {
guard let indexSet = IndexSet(integersIn: sections) else {
fatalError("Invalid section numbers passed to reloadSections(_:). Sections must be non-negative integers")
}
self.reloadSections(indexSet) // Call to existing ObjC API
}
}
Check out the full proposal on GitHub