[swift-evolution] [pitch] One-sided Ranges

Robert Bennett rltbennett at icloud.com
Wed Apr 12 21:11:53 CDT 2017

+1, very nice proposal. I think there was some discussion about this before, glad to see it being fleshed out into a full proposal. My only nitpick is that I think feel like the syntax `sequence[i…]` is awkward because `…` implies the entirety of a range (1…5 includes 1 and 5 and everything in between), but the valid indices of a sequence go from 0 to endIndex, and endIndex cannot be included when slicing. That is, `sequence[i…]` is equivalent to `sequence[i..<sequence.endIndex]`, which feels wrong.

It is possible that this small idiosyncrasy is not worth making the change I'm proposing, which is to introduce both i… and i..< . i… would be used for truly infinite (up to what is machine-representable) ranges, such as `for (x,i) in zip(seq, 1…)` whereas i..< would be used pretty much just for slicing, when there is a known endIndex. Further, I would make both `for (x,i) in zip(seq, ..<)` and `sequence[i…]` compiler errors.

The syntax …i doesn't face these problems because the language doesn't have the notion of left-open ranges (and startIndex is always a valid index whenever startIndex < endIndex), so I take no issue with the leading ellipsis.

Otherwise, very happy with this proposal.

More information about the swift-evolution mailing list