[swift-evolution] [Idea] Switch Statement with Optional Binding

Natthan Leong kar.joon at icloud.com
Fri May 27 03:58:26 CDT 2016


Hi there,

I was wondering if the community would like Swift to have a switch statement with optional binding.

Take this simple dictionary as an example:

let cityIDs = ["Paris": 1, "London": 2]

Currently, this is how things are done if parisCityID is used only once within an if-let statement for only a switch statement: 

if let parisCityID = cityIDs["Paris"] {
    switch parisCityID {
    case 0..<10:
        print("Paris city ID: \(parisCityID)")
    default:
        break
    }
}

And here is the proposed switch statement with optional binding:

switch let parisCityID = cityIDs["Paris"] {
    case 0..<10:
        print("Paris city ID: \(parisCityID)")
    default:
        break
}

With var:

switch var parisCityID = cityIDs["Paris"] {
    case 0..<10:
        parisCityID += 2 // just to demonstrate var
        print("Paris city ID: \(parisCityID)")
    default:
        break
}

Feedback welcomed!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160527/263b6241/attachment.html>


More information about the swift-evolution mailing list