[swift-evolution] Strings in Swift 4

David Sweeris davesweeris at mac.com
Tue Jan 31 15:36:58 CST 2017


> On Jan 31, 2017, at 11:32, Jaden Geller via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 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.

(I think this how it works... semantically, anyway) Since the upper bound isn't specified, it's inferred from the context.

In the first case, the context is as an index into an array, so the upper bound is inferred to be the last valid index.

In the second case, there is no context, so it goes to Int.max. Then, after the "wrong" context has been established, you try to index an array with numbers from the too-large range.

Semantically speaking, they're pretty different operations. Why is it surprising that they have different results?

- Dave Sweeris 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170131/bc7738f3/attachment.html>


More information about the swift-evolution mailing list