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

Natthan Leong kar.joon at icloud.com
Fri May 27 11:49:54 CDT 2016


Nice!

However, the case statements may increase as shown below and I was wondering if it will still be worth it(Greetings core team!) in adding some syntactic sugar as aforementioned...

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

One more aspect that I have thought of is that the proposed switch statement with optional binding may only bind one value unlike if-let i.e.

if let x = foo, y = bar {
    // do something with `x` and `y`
}

// but the following can't happen

switch let x = foo, y = bar {
    // compile error
}

> On May 27, 2016, at 9:25 AM, Kevin Nattinger <swift at nattinger.net> wrote:
> 
> switch cityIDs["Paris"] {
> 	case let .Some(parisCityID) where 0..<10 ~= parisCityID:
> 	print("Paris city ID: \(parisCityID)")
> 	default:
> 	break
> }

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


More information about the swift-evolution mailing list