[Idea] Switch Statement with Optional Binding


#1

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!


(Kevin Nattinger) #2

You can do it today (2.2 and 3.0-master) like this:

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

···

On May 27, 2016, at 1:58 AM, Natthan Leong via swift-evolution <swift-evolution@swift.org> wrote:

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!
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


#3

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@nattinger.net> wrote:

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