I think range(at:) should actually be range(at:length:) with a default length of 1:
extension Collection {
func range(at position: Index, length: Int = 1) -> Range<Index> {
return position ..< index(position, offsetBy: length)
}
}
• • •
I also think the argument label on insert(contentsOf:) is unnecessary and verbose. When inserting a range of elements into a RangeSet of that same element type, there is no ambiguity. The concise spelling is every bit as clear, without the extraneous noise:
var set = RangeSet([0..<5, 10..<15])
set.insert(contentsOf: 5..<7) // Current proposal
set.insert(5..<7) // Original proposal
No one is going to find the version without a label confusing, but people will find the version with the label needlessly tedious.
• • •
Finally, having read the revised proposal more thoroughly now, I am quite convinced that ranges(where:) and ranges(of:) should revert to their original spellings indices(where:) and indices(of:).
Using the example from the proposal:
let str = "Fresh cheese in a breeze"
let allTheEs = str.ranges(of: "e") // Current proposal
let allTheEs = str.indices(of: "e") // Original proposal
The method is asking the collection to return a collection of indices. The resulting collection happens to be a RangeSet, but that is almost irrelevant. The specific type of collection, and its internal representation, don’t actually matter for the purpose of writing algorithms. The important point is that these methods return some collection of indices.
It has been said many times on Swift Evolution that code is read far more often than it is written, and the API design guidelines place clarity at the point of use as the foremost concern. From the perspective of someone reading the code, “ranges of e” doesn’t really convey any meaning. On the other hand, “indices of e” says exactly what it does.
Furthermore, a major part of the revision of this proposal is to change RangeSet from modeling a collection of ranges, to modeling a collection of comparable elements. It seems entirely contrary to that goal if the methods themselves get renamed away from talking about the element type into talking about the ranges.