[swift-evolution] [Proposal] Safer half-open range operator

Luis Henrique B. Sousa lshsousa at gmail.com
Wed Apr 13 09:53:55 CDT 2016


Many thanks @Pyry, it's great to see those changes coming in swift 3. The
examples using *clamp* you mentioned here would do what I'm proposing, but
I totally agree with @Vladimir that we could have a more clear and
*swift-ly* way to concisely wrap those operations.

The behaviour pointed out by him looks very nice and doable to me.

a = [1,2,3]
a[-1..<6] - raises runtime error (right behavior by default, doesn't affect
existing code)
a[truncate: -1..<6] - produces [1,2,3] (the very behaviour I proposed
initially)
a[safe: -1..<6] - produces nil (i.e [T]?) (no runtime errors and makes it
easy to handle unexpected results)

I'd like to hear more opinions before I update my proposal with those new
subscript methods (instead of a different operator, or added as an
alternative considered).
Thanks

- Luis

On Wed, Apr 13, 2016 at 2:11 PM, Vladimir.S via swift-evolution <
swift-evolution at swift.org> wrote:

> Is it really great to have a[a.indices.clamped(to: 0 ..< 5)]
> instead of a clear a[truncate: 0 ..< 5] ?
> and if it is not "a" but "arrayOfSomeValues" we have
> arrayOfSomeValues[arrayOfSomeValues.indices.clamped(to: 0 ..< 5)]
> Don't feel this is nice.
>
> Is it really so wrong to have additional(to "direct" functions like
> "indices.clamped" ) handy and nice-looking methods/subscripts in language?
>
> IMO we all want to have great language. It should be great to code in such
> language. Is it great and enjoyable to have strange long construction
> instead of handy,clear and explicit expression?
> Why don't improve the language in all possible area where we can improve
> it? I believe we should improve.
>
> On 13.04.2016 14:09, Pyry Jahkola via swift-evolution wrote:
>
>> On 11 Apr 2016, at 15:23, Luis Henrique B. Sousa via swift-evolution
>>> <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>
>>> leta =[1,2,3]
>>> letb =a[0..<5]
>>> print(b)
>>>
>>> In the swift-3-indexing-model branch
>> <
>> https://github.com/apple/swift/blob/swift-3-indexing-model/stdlib/public/core/Range.swift#L94
>> >,
>> you can /clamp/ a range just like you could clamp intervals in Swift 2. So
>> the following will work in the way you preferred:
>>
>>      let b = a[a.indices.clamped(to: 0 ..< 5)]
>>
>> It was suggested to extend `Collection` with a subscript like `a[safe: 0
>> ..< 5]` which resembles the current subsequence subscript
>> <
>> https://github.com/apple/swift/blob/swift-3-indexing-model/stdlib/public/core/Collection.swift#L82>.
>> Alternatively,
>> we could bring collections even closer to ranges by extending them with
>> the
>> equivalent `.clamped(to:)` method:
>>
>>      let b = a.clamped(to: 0 ..< 5) // "safe" subsequence
>>
>> — Pyry
>>
>>
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160413/43a359f4/attachment.html>


More information about the swift-evolution mailing list