[swift-evolution] Optional safe subscripting for arrays

davesweeris at mac.com davesweeris at mac.com
Mon Feb 1 17:10:45 CST 2016


Ok, if the subscript label on the second one isn’t the same as the first one, it works. I’m still not sure why what I wrote earlier today was ambiguous, but this seems to work:
extension Array {
    subscript(failableLookup idx: Index) -> Element? {
        get { return (startIndex ..< endIndex) ~= idx ? self[idx] : nil }
        set { if (startIndex ..< endIndex) ~= idx && newValue != nil { self[idx] = newValue! } }
    }
}
extension Array where Element: NilLiteralConvertible {
    subscript(nilConvertible idx: Index) -> Element? {
        get { return (startIndex ..< endIndex) ~= idx ? self[idx] : nil }
        set { if (startIndex ..< endIndex) ~= idx { self[idx] = newValue ?? Element(nilLiteral: ())} }
    }
}

Seems kinda “hacky”, though, to need the 2nd set argument labels.

Anyway, I hope this helps.

- Dave Sweeris

> On Feb 1, 2016, at 00:53, Rudolf Adamkovič via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi Maximilian,
> 
> ah, I see. This is a show stopper then!
> 
> From what I imagine, this should not type-check:
> 
> var array = [1]
> array[ifExists: 0] = nil
> 
> … and this should set array[0] to nil:
> 
> var array: [Int?] = [1]
> array[ifExists: 0] = nil
> 
> Is it not possible to implement such setter in Swift?
> 
> R+
> 
>> On 1 Feb 2016, at 00:07, Maximilian Hünenberger <m.huenenberger at me.com <mailto:m.huenenberger at me.com>> wrote:
>> 
>> The setter of the subscript should be:
>> 
>> set {
>>     if self.indices ~= index && newValue != nil {
>>         self[index] = newValue!
>>     }
>> }
>> 
>> Since "newValue" is of type "Element?".
>> 
>> It seems that this subscript could also be added to "CollectionType" and "MutableCollectionType".
>> 
>> The setter is weird because you can use an optional element:
>> 
>> var arr = [1]
>> // is valid but doesn't set the first element
>> arr[ifExists: 0] = nil
>> 
>> var arr2: [Int?] = [1]
>> arr2[ifExists: 0] = nil // changes nothing
>> arr2[ifExists: 0] = .Some(nil) // sets first element to nil : arr2 == [nil]
>> 
>> I don't know whether a setter should be added at all.
>> 
>> - Maximilian
>> 
>> Am 31.01.2016 um 23:38 schrieb Rudolf Adamkovič via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
>> 
>>> All right, I put together a proposal:
>>> 
>>> https://github.com/salutis/swift-evolution/blob/master/proposals/XXXX-runtime-safe-array-subscripting.md <https://github.com/salutis/swift-evolution/blob/master/proposals/XXXX-runtime-safe-array-subscripting.md>
>>> 
>>> … and opened a PR:
>>> 
>>> https://github.com/apple/swift-evolution/pull/133 <https://github.com/apple/swift-evolution/pull/133>
>>> 
>>> Let’s see how this goes.
>>> 
>>> R+
> 
> _______________________________________________
> 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/20160201/6353e6a7/attachment.html>


More information about the swift-evolution mailing list