1

Since case: blocks auto-break, Swift provides the fallthrough keyword. This is of limited use because it doesn't fallthrough to the next conditional test, it bypasses the next test and just executes the code from the next test. Is it possible to have a switch statement perform the code in multiple cases while also performing the conditionals?

As an example from the Swift documentation, what if I need the code below to execute every block that applies to the given point?

let somePoint = (0, 0)
switch somePoint {
case (0, 0):
    print("\(somePoint) is at the origin")
case (_, 0):
    print("\(somePoint) is on the x-axis")
case (0, _):
    print("\(somePoint) is on the y-axis")
case (-2...2, -2...2):
    print("\(somePoint) is inside the box")
default:
    print("\(somePoint) is outside of the box")
}

As written it will print only the first description even though multiple actually apply. Using fallthrough after each test causes every case block to execute.

user800268
  • 77
  • 8
  • 1
    That's not what a switch statement is for – you can use multiple if statements. – Martin R Jan 19 '18 at 21:17
  • Sure, it's just a lot uglier to do it with if statements. If there were something like fallthroughAndTest it would be great for this. The Swift switch is already so turbo-charged I was hoping there was a technique for this. – user800268 Jan 19 '18 at 21:28
  • I don't think there is. – Martin R Jan 19 '18 at 21:31
  • 1
    This is an interesting idea. `switch` used to be an exclusive construct, where one value was compared against a definite set of cases. In that case, "`fallthroughAndTest`" wouldn't make sense, because the test would always fail (e.g. if an integer `i` matched `case 0`, it will be guaranteed to fail the test for `case 1`). So only having `fallthrough` made sense, but now that the exclusivity restriction has been lifted, it would make sense to introduce a new construct like this – Alexander Jan 20 '18 at 00:40

1 Answers1

3

A switch statement compares a value against patterns and executes the code based on the first successful match.

If your intention is to match against multiple patterns and execute code for all matches then use multiple if statements. The same case patterns can be used:

if case (0, 0) = somePoint {
    print("\(somePoint) is at the origin")
}
if case (_, 0) = somePoint {
    print("\(somePoint) is on the x-axis")
}
// ...
if case (-2...2, -2...2) = somePoint {
    print("\(somePoint) is inside the box")
}
// ...
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • If the switch statement is as you describe it then doesn't the fallthrough statement corrupt it? – user800268 Jan 19 '18 at 21:34
  • Working my way through the Swift docs. Interestingly the "Control Flow" section that describes "if" doesn't mention this very useful "if case." – user800268 Jan 19 '18 at 21:38
  • 1
    @user800268: That is the [`case-condition`](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/case-condition) in the Language Reference. – Martin R Jan 19 '18 at 21:41
  • 1
    @user800268: Examples here: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html, look for "Optional Pattern". – Also explained here: https://stackoverflow.com/questions/30720289/swift-2-pattern-matching-in-if. – Martin R Jan 19 '18 at 21:47