2
var number = 10

var bool = false

switch (number, bool) {

case (_, false):
    print("bool is false")
case (10, _):
    print("number is 10")
default:
    print("whatever")
}

The above would print "bool is false"

var number = 10

var bool = false

switch (number, bool) {
case (10, _):
    print("number is 10")
case (_, false):
    print("bool is false")
default:
    print("whatever")
}

The above would print "number is 10"

To further test this I moved the default case to the top and got:

error: additional 'case' blocks cannot appear after the 'default' block of a 'switch'

Questions:

So is this standard Foundation behavior? I looked at the library but didn't find anything about the sequence of cases.

Additionally does this mean two cases can't be identical but can be overlapping...and it would just fall through whichever case happened first?

mfaani
  • 33,269
  • 19
  • 164
  • 293
  • 4
    `switch` is in Swift, not in Foundation, and yes, cases are evaluated from top down. Order matters. :) – Eric Aya Nov 17 '17 at 13:42
  • 4
    Note this has nothing to do with enumerations or Foundation, it's simply pattern matching in a `switch` statement; the behaviour of which is specified by the language, see: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Statements.html#//apple_ref/swift/grammar/guard-statement – Hamish Nov 17 '17 at 13:47
  • 2
    Specifically: "*Although the actual execution order of pattern-matching operations, and in particular the evaluation order of patterns in cases, is unspecified, pattern matching in a `switch` statement behaves as if the evaluation is performed in source order—that is, the order in which they appear in source code. As a result, if multiple cases contain patterns that evaluate to the same value, and thus can match the value of the control expression, the program executes only the code within the first matching case in source order.*" – Hamish Nov 17 '17 at 13:49
  • 1
    @Hamish Worth mentioning that if needed he can also add the keyword fallthrough to make it evaluate the next case. https://stackoverflow.com/questions/31782316/how-to-create-a-hex-color-string-uicolor-initializer-in-swift/31782490?s=1|22.3981#31782490 – Leo Dabus Nov 17 '17 at 14:19

1 Answers1

3

The switch statement is part of the Swift language, not any particular library, and yes, order matters.

It might help to think of a switch statement as a shortcut for a series of "if-then-else" statements. Say I have the following statement:

switch(number) {
case (1):
    doSomethingForOne()
    break
case (2):
    doSomethingForTwo()
    break
default:
    doSomethingForDefault()
    break
}

This is functionally equivalent to the following code:

if(number == 1) {
    doSomethingForOne()
}
else if (number == 2) {
    doSomethingForTwo()
}
else {
    doSomethingForDefault()
}

You see that the default stands in for the else. It wouldn't make sense to add an else if() after that else, because it will never execute (the else 'eats' the input)

redsoxfantom
  • 918
  • 1
  • 14
  • 29
  • I asked 2 questions. You didn't answer if they can be identical or not. Though I just tried and oddly it works, though you would get a warning... – mfaani Nov 17 '17 at 14:11
  • 3
    Note that break (and parentheses) it is not needed. Btw It is Swift naming convention to use camelCase not snake_case – Leo Dabus Nov 17 '17 at 14:43
  • @LeoDabus, thanks for the heads up. I'm not a Switch guy, so I made my best guesses – redsoxfantom Nov 17 '17 at 16:08