In Swift, why does a switch case never run into the next case by default, and what exactly does the fallthrough keyword do?
answer
- first match, then the switch ends
- no trailing break needed
- opt in one case at a time
- next pattern is not checked
- cannot fall into new bindings
basics
~20 sA Swift switch ends as soon as the first matching case body finishes, so no break is needed. fallthrough opts in: control jumps straight into the next case's body without checking that case's pattern or where clause.
solid answer
~50 sSwift's `switch` runs exactly one case body - the first whose pattern matches - and then the whole statement ends, so you never write `break` just to stop fall-through, and an empty case body is a compile error rather than a silent merge with the next case. When you do want C-style behaviour, `fallthrough` transfers control directly into the body of the next case in source order; it does not evaluate that case's pattern or `where` clause, so the next body runs even if its pattern would not match. The compiler restricts it: it is only allowed inside a `switch`, it cannot appear in the last case ("'fallthrough' without a following 'case' or 'default' block"), and it cannot enter a case that binds a variable the current case does not bind. To share one body between values, a compound case such as `case "a", "A":` is usually clearer than `fallthrough`.
code
swift · 20 linesfunc permissions(for role: String) -> [String] {
var granted: [String] = []
switch role {
case "admin":
granted.append("delete")
fallthrough
case "editor":
granted.append("edit")
fallthrough
case "viewer":
granted.append("read")
default:
break
}
return granted
}
print(permissions(for: "admin")) // ["delete", "edit", "read"]
print(permissions(for: "editor")) // ["edit", "read"]
print(permissions(for: "guest")) // []go deeper
Recall that a Swift switch runs one case body and stops, with no break required, and that fallthrough is the explicit opt-in.
Explain that fallthrough jumps into the next body without testing its pattern, and name the compiler's limits: not in the last case, not into new bindings.
In review, question every fallthrough: confirm the author knows the next pattern is skipped, and prefer compound cases when bodies are simply shared.
There is no policy decision of substance here; a lead may simply ask that any fallthrough carry a comment, since it makes case order meaningful.
## No implicit fall-through In Swift, a `switch` statement evaluates its cases in order, runs the body of the **first** case whose pattern matches, and then finishes. Control never drops from the bottom of one case body into the next. Two rules follow directly: - **No trailing `break`.** A case body ends the `switch` by reaching its last statement. A `break` inside a case is still legal - to leave the case early, or as the single statement of a case that should do nothing - but it is never needed to stop fall-through. - **No empty case bodies.** Every case must contain at least one executable statement. Writing `case "a":` directly above `case "A":`, expecting the first to share the second's body, fails with "'case' label in a 'switch' must have at least one executable statement". Swift's answer for shared bodies is a **compound case**, `case "a", "A":` (how case patterns combine is covered in the Swift lesson on switch patterns). The design choice is safety: a missing `break` cannot silently run a second case, a classic bug in languages with implicit fall-through. ## What `fallthrough` does `fallthrough` is the explicit opt-in. Its behaviour is narrow and easy to state precisely: 1. Execution of the current case body stops at the `fallthrough` statement. 2. Control moves to the **first statement of the next case body in source order** - the next `case` or the `default`. 3. That case's **pattern and `where` clause are not evaluated**. The body runs whether or not the value would have matched it. 4. The next body may itself end with `fallthrough`, chaining further; otherwise the `switch` ends after it. The third point is the one candidates get wrong. With `switch 5 { case 5: ...; fallthrough; case 100: ... }`, the `case 100` body runs, even though 5 is not 100. `fallthrough` is a jump to the next body, not a request to "keep matching". ## Rules the compiler enforces | Rule | Diagnostic | |---|---| | Only inside a `switch` | "'fallthrough' is only allowed inside a switch" | | Not in the final case | "'fallthrough' without a following 'case' or 'default' block" | | Cannot enter a case that binds a variable the current case does not bind | "'fallthrough' from a case which doesn't bind variable 'n'" | The reference manual words the third rule as a flat ban on falling into a case whose pattern contains value bindings. The Swift 6.4 compiler is slightly more permissive: when the current case binds the same names with the same types, as in `case (let a, 2): fallthrough` followed by `case (_, let a):`, the jump compiles, and for the value `(1, 2)` the second body sees `a == 1` carried over rather than re-bound to 2. When the next case would bind a name the current case cannot supply, it is rejected, because that variable would have no value. `fallthrough` does not have to be the last statement in a case body; it can sit inside an `if` within the case. Any statements after it on the same path are unreachable, and the compiler warns about them. ## A real use: cumulative permissions `fallthrough` is rare in Swift code, and its legitimate uses share a shape: each case adds something and then wants everything the following cases add. Role-based permissions are the textbook case: ```swift switch role { case "admin": granted.append("delete") fallthrough case "editor": granted.append("edit") fallthrough case "viewer": granted.append("read") default: break } ``` An admin gets `["delete", "edit", "read"]`, an editor `["edit", "read"]`, and any other role nothing. The source order of the cases now carries meaning, so reordering them silently changes behaviour - worth a comment in real code. ## `fallthrough` versus a compound case | Need | Prefer | |---|---| | Several values share exactly one body | a compound case: `case "a", "A":` | | A case does its own work, then everything the next case does | `fallthrough` | | Values share work, but the order of cases should not matter | a helper function both cases call | ## What reviewers look for - A `fallthrough` whose author expected the next case's pattern to be re-checked. - Empty case bodies written out of habit from languages with implicit fall-through. - A chain of `fallthrough` statements where a compound case or a small function would remove the dependence on case order.
- Does fallthrough check the where clause of the case it falls into?No. `fallthrough` moves control to the first statement of the next case body and skips that case's pattern and `where` clause entirely. If the next body should only run under a condition, test it explicitly inside the current case before writing `fallthrough`.
- How do you make two values share one case body without fallthrough?List both patterns in one case, separated by a comma: `case "a", "A":`. The body runs when either pattern matches, and the switch still ends after it. This is the idiomatic replacement for the empty case bodies that implicit fall-through allows elsewhere.
- Every Swift case needs a break at the end to stop fall-through
- fallthrough re-checks the next case's pattern before running its body
- An empty case body falls into the next case
- fallthrough jumps to the next case that matches the value
- fallthrough is allowed in the last case to leave the switch