[swift-evolution] Strings in Swift 4

James Froggatt james.froggatt at me.com
Mon Jan 30 19:04:01 CST 2017

Going to try resending this - my client doesn't seem to give me a proper email address to reply to (only some gmane mangled address which got me a delivery failure); sorry for the lack of a direct reply.


Exactly. I'm not too familiar with range subscripting on arrays, but if it's anything like a regular Int subscript, I'd expect…

let outOfBoundsSubrange = items[items.startIndex...items.endIndex]

…to give an out of bounds error, since (in my mental model) it tries to access items[items.endIndex].

If ‘items.startIndex...’ represents an unbounded range (one with a unknown / subscript-defined endIndex), I can't see enough use-cases for it outside of subscripting to justify its existence as a type - it can't even conform to sequence, since there is no endIndex, making it essentially just a number and a promise of how APIs will use it. If we pretend an unbounded range is infinite by conforming it to sequence, people will inevitably use it like one.

If this syntax represents an infinite range, I'd expect it to similarly give an error when used to subscript a non-infinite array-like type, but it would at least be independently useful outside of collections as a way to generate an infinite sequence.

As a further point, what if we also implemented prefix comparison operators? Which of the following looks more like an infinite range, and which looks more like an unbounded one? I'll leave the answer up to the reader:

let indices = >oneDigitNums.startIndex

let indices = oneDigitNums.startIndex...

------------ Begin Message ------------ 
Group: gmane.comp.lang.swift.evolution 
MsgID: <CD036AEB-35F5-41C9-8E7F-C223EDC6A3A5 at gmail.com> 

> On Jan 30, 2017, at 11:35 AM, Dave Abrahams via swift-evolution <swift-evolution-m3FHrko0VLzYtjvyW6yDsg at public.gmane.org> wrote:
> Why should that be out-of-bounds?  Whether it is out-of-bounds would
> depend on what items is.  If it's an array, that should be equivalent to
> let x = items[items.startIndex..<items.endIndex]

It seems to me that `items[0…]` would be equivalent to `items[0…Int.max]` if we’re going to treat `0…` as an “infinite" range, no? Otherwise, we’re either giving subscript of InfiniteRange types special behavior or we’re making subscript ignore past-the-end indices; `”hello”.characters[0…10]` would need to return the same as “hello”.characters[0…4]` to be consistent.

------------- End Message ------------- 

From James

More information about the swift-evolution mailing list