> ## 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 Closure Expression

A closure expression is an unnamed, self-contained block of executable code written in a lightweight, inline syntax. It acts as a first-class citizen in Swift, meaning it can be assigned to variables, passed as an argument, or returned from a function. Closure expressions lexically capture and store references to variables and constants from their surrounding context.

## Base Syntax

The general form of a closure expression includes a parameter list, a return type, the `in` keyword, and the executable body.

```swift theme={"dark"}
{ (parameters) -> ReturnType in
    // statements
}
```

* **`parameters`**: A comma-separated list of inputs. These cannot have default values, but variadic parameters and `inout` parameters are supported.
* **`ReturnType`**: The data type the closure evaluates to and returns.
* **`in`**: A structural keyword that separates the closure's signature (parameters and return type) from its executable body.

## Syntax Optimization and Reduction

Swift's compiler utilizes type inference and syntactic sugar to reduce the verbosity of closure expressions. A fully explicit closure can be systematically reduced based on its contextual type.

**1. Fully Explicit Closure**

```swift theme={"dark"}
let add: (Int, Int) -> Int = { (a: Int, b: Int) -> Int in
    return a + b
}
```

**2. Inferring Type From Context**
When a closure is assigned to a strictly typed variable or passed as a function argument, the compiler infers the parameter types and the return type. The type declarations and the `->` operator can be omitted.

```swift theme={"dark"}
let add: (Int, Int) -> Int = { a, b in
    return a + b
}
```

**3. Implicit Returns from Single-Expression Closures**
If the closure body consists of a single expression, the compiler implicitly returns the evaluated result of that expression. The `return` keyword can be omitted.

```swift theme={"dark"}
let add: (Int, Int) -> Int = { a, b in a + b }
```

**4. Shorthand Argument Names**
Swift automatically provides shorthand argument names (`$0`, `$1`, `$2`, etc.) representing the closure’s arguments in order. When using shorthand arguments, the parameter list and the `in` keyword are entirely omitted.

```swift theme={"dark"}
let add: (Int, Int) -> Int = { $0 + $1 }
```

## Trailing Closure Syntax

When a closure expression is passed as the final argument to a function, it can be written as a trailing closure. A trailing closure is written outside and immediately after the function call's closing parenthesis.

```swift theme={"dark"}
// Standard inline syntax
transform(value: 10, operation: { $0 * 2 })

// Trailing closure syntax
transform(value: 10) { $0 * 2 }
```

If the closure expression is the *only* argument provided to the function, the parentheses following the function name can be omitted entirely.

```swift theme={"dark"}
// Function signature: func execute(action: () -> Void)
execute { print("Executing...") }
```

**Multiple Trailing Closures**
If a function takes multiple closures as its final arguments, the first trailing closure omits its argument label, while subsequent trailing closures are written outside the parentheses alongside their respective argument labels.

```swift theme={"dark"}
// Function signature: func animate(duration: Double, animations: () -> Void, completion: () -> Void)

animate(duration: 0.3) {
    // animations closure body
} completion: {
    // completion closure body
}
```

## Capture Lists

By default, closures capture their surrounding context by reference. A capture list explicitly overrides this behavior, defining the precise rules for how variables are captured. The capture list is placed at the very beginning of the closure expression, enclosed in square brackets.

Capture lists serve two primary purposes:

1. **Memory Management:** Breaking strong reference cycles when capturing reference types (class instances) by specifying `weak` or `unowned` modifiers.
2. **Value Capture:** Forcing a value type to be captured *by value* rather than by reference. This freezes the variable's state, capturing an immutable copy of the value at the exact moment the closure is created.

```swift theme={"dark"}
// Capturing a reference type weakly and a value type by value
{ [weak self, count] (parameter: String) -> Void in
    // 'self' is an optional reference
    // 'count' is an immutable copy of the value at creation time
}
```

If the closure utilizes shorthand arguments or implicit returns, the `in` keyword must be reintroduced to separate the capture list from the closure body.

```swift theme={"dark"}
{ [weak self] in self?.handle($0) }
```

<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>
