> ## 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 Nil-Coalescing

The `??` (nil-coalescing) operator is a binary operator that unwraps an `Optional<T>` if it contains a value, or returns a fallback default value of type `T` if the optional evaluates to `nil`.

## Syntax

```swift theme={"dark"}
let result = optionalExpression ?? defaultExpression
```

Semantically, the `??` operator is a concise shorthand for the ternary conditional operator combined with forced unwrapping:

```swift theme={"dark"}
// The ?? operator
a ?? b

// Is equivalent to:
a != nil ? a! : b
```

## Type Constraints

For the compiler to resolve the `??` operator, strict type constraints apply:

1. The left-hand side (`optionalExpression`) must be of type `Optional<T>`.
2. The right-hand side (`defaultExpression`) must evaluate to type `T` (the exact wrapped type of the left-hand side).
3. The resulting expression evaluates to a non-optional `T`.

*Note: It is syntactically valid for the right-hand side to also be an `Optional<T>`. In this scenario, the resulting expression remains an `Optional<T>`, allowing for nil-coalescing chains (`a ?? b ?? c`).*

## Short-Circuit Evaluation

The `??` operator utilizes short-circuit evaluation. The right-hand side expression is evaluated lazily. If the left-hand side optional contains a value, the right-hand side is completely ignored and never executed.

This behavior is achieved in the Swift Standard Library by defining the `defaultValue` parameter as an `@autoclosure`.

## Standard Library Signature

Under the hood, the operator is defined with the following function signature:

```swift theme={"dark"}
public func ?? <T>(
    optional: T?, 
    defaultValue: @autoclosure () throws -> T
) rethrows -> T
```

* `T?`: The optional value being evaluated.
* `@autoclosure () throws -> T`: The default value, wrapped in an automatic closure to defer execution until it is strictly necessary (i.e., when `optional` is `.none`).
* `rethrows`: Propagates any errors thrown by the `defaultValue` expression, provided the expression is actually evaluated.

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