[swift-evolution] Optional safe subscripting for arrays

Maximilian Hünenberger m.huenenberger at me.com
Sun Jan 31 17:07:03 CST 2016


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>:
> 
> All right, I put together a proposal:
> 
> 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
> 
> Let’s see how this goes.
> 
> R+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160201/41a68543/attachment.html>


More information about the swift-evolution mailing list