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

Vladimir.S svabox at gmail.com
Wed Apr 13 08:11:14 CDT 2016


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
>


More information about the swift-evolution mailing list