> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift Switch Expression

A `switch` expression in Swift is a control flow construct that evaluates a control value and directly yields the result of the matched case, allowing the entire `switch` block to evaluate to a single value. Introduced in Swift 5.9 (SE-0380), it transforms the traditional `switch` statement into an expression, enabling its use on the right-hand side of assignments, within return statements, or as property initializers.

```swift theme={"dark"}
enum Status {
    case success, failure
}

let currentStatus = Status.success

let message = switch currentStatus {
case .success: "Operation successful"
case .failure: "Operation failed"
}

print(message) // Output: Operation successful
```

## Technical Mechanics and Compiler Rules

To use a `switch` as an expression, the compiler enforces strict structural and type constraints:

### 1. Single Expression Requirement

Each `case` branch (including `default`) must contain exactly one expression, with the sole exception being a `throw` statement. The evaluated result of the single expression becomes the yielded value of the entire `switch` block. You cannot use multiple statements, variable declarations, or complex imperative logic inside a case when utilizing `switch` as an expression.

### 2. Type Homogeneity and Inference

The Swift compiler must be able to determine a single, unified return type for the entire `switch` expression. By default, every branch must evaluate to the exact same type. The Swift type checker does *not* automatically compute a least upper bound (such as a common superclass or an existential type) if the branches yield different types. If the branches evaluate to different types, the compiler will emit a type mismatch error unless an explicit contextual type is provided by the developer.

```swift theme={"dark"}
class BaseView {}
class TextView: BaseView {}
class ImageView: BaseView {}

enum ViewState {
    case text, image
}

let state = ViewState.text

// Explicit contextual typing (BaseView) is required for differing subclass branches
let view: BaseView = switch state {
case .text: TextView()
case .image: ImageView()
}
```

### 3. Implicit Returns

When a `switch` is used as an expression, the `return` keyword is strictly omitted within the individual cases. The expression itself implicitly yields its value. This applies both to variable assignment and when the `switch` expression is the sole expression inside a function or closure.

```swift theme={"dark"}
func mapStatus(_ code: Int) -> String {
    // The switch expression implicitly returns its evaluated string
    switch code {
    case 200: "OK"
    case 404: "Not Found"
    default: "Unknown"
    }
}

print(mapStatus(200)) // Output: OK
```

### 4. Exhaustiveness

Like `switch` statements, `switch` expressions must be exhaustive. Every possible value of the control expression's type must be covered by a case or a `default` branch. Because an expression must guarantee a return value, the compiler will fail to build if a potential path does not yield a value.

### 5. Diverging Execution Paths (`Never` and `throw`)

A branch in a `switch` expression is permitted to diverge from the inferred return type if it evaluates to the `Never` type or executes a `throw` statement. The compiler recognizes that these paths will halt execution rather than return a value, preserving the type safety of the overall expression.

If a branch evaluates a throwing expression (such as calling a throwing function), the `try` keyword can be placed directly on the throwing sub-expression inside the specific branch. Alternatively, the `try` keyword can be placed on the `switch` keyword itself (e.g., `try switch`), which applies the throwing context to the entire expression. Using a `throw` statement directly inside a branch inherently throws and propagates the error.

```swift theme={"dark"}
enum Environment {
    case production, development, testing, unknown
}

enum ConfigurationError: Error {
    case missing
}

func getTestConfig() throws -> String {
    return "test_config.json"
}

let environment = Environment.testing

do {
    let configuration = switch environment {
    case .production: "prod_config.json"
    case .development: "dev_config.json"
    case .testing: try getTestConfig() // 'try' is placed directly on the sub-expression
    case .unknown: throw ConfigurationError.missing // Halts execution and throws directly
    }
    print(configuration) // Output: test_config.json
} catch {
    print("Error: \(error)")
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Swift Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
