# What's the harm in not knowing that a switch statement's pattern-matching order is undefined?

**URL:** <https://forums.swift.org/t/whats-the-harm-in-not-knowing-that-a-switch-statements-pattern-matching-order-is-undefined/35259>\
**Category:** Using Swift\
**Tags:** control-flow, switch\
**Created:** [April 10, 2020, 6:55pm UTC](https://forums.swift.org/t/whats-the-harm-in-not-knowing-that-a-switch-statements-pattern-matching-order-is-undefined/35259 "2020-04-10T18:55:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr-fixit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mr-fixit/32/8721_2.png) [@mr-fixit](https://forums.swift.org/u/mr-fixit)\
**Post date:** [April 10, 2020, 6:55pm UTC](https://forums.swift.org/t/whats-the-harm-in-not-knowing-that-a-switch-statements-pattern-matching-order-is-undefined/35259/1 "2020-04-10T18:55:10Z")

</div>

I just read this in “The Swift Programming Language”: (5.2 beta, p 1005)

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.

Is it possible to get ‘something unexpected’ by assuming the ‘evaluation order of patterns’ **is** performed in source order?

In other words, can a pattern-match have a side-effect, and can the Nth case's pattern match or statements could be affected by the (N+m)th's pattern-match side-effect?

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 10, 2020, 7:14pm UTC](https://forums.swift.org/t/whats-the-harm-in-not-knowing-that-a-switch-statements-pattern-matching-order-is-undefined/35259/2 "2020-04-10T19:14:48Z")

</div>

When you use an expression in a `case` label, Swift will evaluate that expression to get the value to match against. If that evaluation has side effects, they may be visible during the evaluation of other cases. For instance, if you had:

```swift
switch () {
case print("foo"): break
case print("bar"): break
}

```

The compiler could in theory print "bar" before "foo".

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 10, 2020, 7:27pm UTC](https://forums.swift.org/t/whats-the-harm-in-not-knowing-that-a-switch-statements-pattern-matching-order-is-undefined/35259/3 "2020-04-10T19:27:47Z")

</div>

For a concrete example of how evaluation order might be different from source order, try this:

```swift
enum Foo {
  case a(Int)
  case b(Int)
}

func printAndReturn(_ x: Int) -> Int {
  print(x)
  return(x)
}

let x = Foo.b(2)
switch x {
  case .a(printAndReturn(0)): break
  case .a(printAndReturn(1)): break
  case .b(printAndReturn(2)): break
  case .b(printAndReturn(3)): break
  default: break
}

```

The `printAndReturn(0)` and `printAndReturn(1)` calls inside the `.a` branches will not execute, because the compiler generates an optimized decision tree where the `.a` and `.b` cases are partitioned first before trying to evaluate any subpatterns.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [April 10, 2020, 7:47pm UTC](https://forums.swift.org/t/whats-the-harm-in-not-knowing-that-a-switch-statements-pattern-matching-order-is-undefined/35259/4 "2020-04-10T19:47:58Z")

</div>

> [@mr-fixit](#):
>
> (5.2 beta, p 1005)

If you're referring to the e-book version, note that page number changes depending on the screen size. It'd be better to use chapter number & section title.

---

<div class="post-metadata">

**Author:** ![mr-fixit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mr-fixit/32/8721_2.png) [@mr-fixit](https://forums.swift.org/u/mr-fixit)\
**Post date:** [April 13, 2020, 1:23pm UTC](https://forums.swift.org/t/whats-the-harm-in-not-knowing-that-a-switch-statements-pattern-matching-order-is-undefined/35259/5 "2020-04-13T13:23:05Z")

</div>

Thanks for the example, Joe. I hadn't imagined calling a method in the pattern-match expression, I normally use constants.

I extended your example slightly to drive the point home, it shows how the compiled code is first checking for `.a` vs `.b`, then doing a pared-down comparision.

```swift
enum Foo {
    case a(Int)
    case b(Int)
}

func printAndReturn(_ x: Int) -> Int {
    print("pr: \(x)")
    return(x)
}

for i in 0...4 {
    let a_and_b = [Foo.a(i), Foo.b(i)]
    for x in a_and_b {
        print("------ \(x) -----")
        switch x {
        case .a(printAndReturn(0)): print ("case 0"); break
        case .a(printAndReturn(1)): print ("case 1"); break
        case .b(printAndReturn(2)): print ("case 2"); break
        case .b(printAndReturn(3)): print ("case 3"); break
        default: print ("case default"); break
        }
    }
}

```

and got

```nohighlight
pr: 0
case 0
------ b(0) -----
pr: 2
pr: 3
case default
------ a(1) -----
pr: 0
pr: 1
case 1
------ b(1) -----
pr: 2
pr: 3
case default
------ a(2) -----
pr: 0
pr: 1
case default
------ b(2) -----
pr: 2
case 2
------ a(3) -----
pr: 0
pr: 1
case default
------ b(3) -----
pr: 2
pr: 3
case 3
------ a(4) -----
pr: 0
pr: 1
case default
------ b(4) -----
pr: 2
pr: 3
case default

```
