[swift-evolution] Optional safe subscripting for arrays

Rudolf Adamkovič salutis at me.com
Sat Jan 16 14:44:31 CST 2016


Hi there!

From time to time, I find myself using safe subscripting, e.g.:

let array = [1, 2, 3]

array[safe: 0] // 1
array[safe: 1] // 2
array[safe: 2] // 3
array[safe: 3] // nil

… with the following implementation:

subscript (safe index: Int) -> Element? {
    return self.indices ~= index ? self[index] : nil
}

I was wondering … wouldn’t it be handy to have this in the standard library?

Best regards,
Rudolf Adamkovic

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160116/be2b7836/attachment.html>


More information about the swift-evolution mailing list