[swift-evolution] Strings in Swift 4
Matthew Johnson
matthew at anandabits.com
Thu Feb 2 10:27:33 CST 2017
> On Feb 2, 2017, at 9:45 AM, Dave Abrahams <dabrahams at apple.com> wrote:
>
>
>
> Sent from my iPad
>
> On Feb 2, 2017, at 7:11 AM, Matthew Johnson <matthew at anandabits.com <mailto:matthew at anandabits.com>> wrote:
>
>>>
>>>>
>>>> Furthermore, we emphatically do *not* need to make the distinction you claim between “infinite” and “incomplete” ranges, which *is* needless hairsplitting.
>>>
>>> Strongly disagree, unless you can describe the semantics of the type WITHOUT giving it different semantics depending on how it is used.
>>
>> This is the point that convinced me. I’m going to take a closer look at Brent’s `RangeExpression` design which I must admit I only skimmed in the earlier discussion.
>
> We already have exactly this situation with CountableRange (which will merge with Range when conditional conformances land). When used as a Collection, it means "every index value starting with the lowerBound and ending just before the upperBound". When used for slicing, it means, roughly, "take every one of the collection's indices that are in bounds.”
I don’t see how the behavior of the following code means roughly “take every one of the collection’s indices that are in bounds”. Can you elaborate?
let range = 0..<20
let array = [1, 2, 3]
let slice = array[range] // trap on index out of bounds
> These are not the same thing. A collection's indices need not include every expressible value of the Index type between startIndex and endIndex.
Sure, but it does appear as if the behavior of slicing assumes that the upper and lower bounds of the range provided are valid indices.
Xiaodi had convinced me that a one-sided range must take a position on whether or not it has an infinite upper bound in order to make sense, but now you’ve changed my mind back.
>
> The whole point of the name RangeExpression is to acknowledge this truth: ranges in Swift bits of syntax whose meaning is given partly by how they are used.
This makes sense and is roughly the position I started with. I should have read through the archives of this thread more before jumping into the middle of the discussion - I was missing some important context. I apologize for not doing so.
The name `RangeExpression` does a good job of indicating why it’s ok for instances to sometimes behave as if they have an infinite upper bound and other times not depending on context.
> In fact, now that I say it, in that respect ranges are not all that different any other type: the meaning of a Double or an Array<String> or a Bool is also interpreted by the methods to which it is passed, and can have completely different results depending on that context.
>
> chillaxing-ly y'rs,
>
> Dave
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170202/aca15939/attachment.html>
More information about the swift-evolution
mailing list