[swift-evolution] Strings in Swift 4

Dave Abrahams dabrahams at apple.com
Tue Jan 31 14:46:57 CST 2017


on Tue Jan 31 2017, Jaden Geller <jaden.geller-AT-gmail.com> wrote:

>>>> On Jan 30, 2017, at 11:35 AM, Dave Abrahams via swift-evolution <swift-evolution at swift.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.
>> 
>> What is it they say about “a foolish consistency?” ;-)
>> 
>> More seriously, I think you may be viewing these ranges the wrong way
>> around.
>> 
>>    0...
>> 
>> is not a range with an upper bound of infinity (which is, after all, not
>> a number!); it's a range with *no* upper bound.  When you use the range
>> for slicing, the collection substitutes its own upper bound.
>> 
>> HTH,
>> 
>> -- 
>> -Dave
>
> I think that is perfectly reasonable, but then it seems weird to be
> able to iterate over it (with no upper bound) independently of a
> collection). It would surprise me if
> ```
> for x in arr[arr.startIndex…] { print(x) }
> ```
> yielded different results than
> ```
> for i in arr.startIndex… { print(arr[i]) } // CRASH
> ```
> which it does under this model.

That makes perfect sense to me.  The first example visually shows the range
being constrained by the bounds of `arr`, whereas in the second example
it is not.

-- 
-Dave


More information about the swift-evolution mailing list