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

The `==` (equality) operator is a binary infix operator that evaluates whether two operands are semantically equivalent, returning a `Bool`. In Swift, `==` is fundamentally a standard operator that can be overloaded as a static or global function. It is not strictly bound to a specific protocol, nor does it inherently require its operands to be of the exact same type.

## Operator Overloading

Developers can define the `==` operator for any type without conforming to any protocols. It can be implemented to compare instances of the same type, or it can be overloaded to compare two entirely distinct types.

```swift theme={"dark"}
struct TypeA { let value: Int }
struct TypeB { let value: Int }

// Global overload comparing two different types
func == (lhs: TypeA, rhs: TypeB) -> Bool {
    return lhs.value == rhs.value
}
```

## The `Equatable` Protocol

While `==` can be implemented independently, conforming to the `Equatable` protocol standardizes equality checks across the language. Conformance is required to satisfy generic constraints (such as using `Array.contains(_:)`) and to inherit the default implementation of the `!=` (inequality) operator.

```swift theme={"dark"}
protocol Equatable {
    static func == (lhs: Self, rhs: Self) -> Bool
}
```

When a type conforms to `Equatable`, it must implement `==` as a static method where both the left-hand side (`lhs`) and right-hand side (`rhs`) operands are of the conforming type (`Self`).

## Compiler Synthesis

The Swift compiler can automatically synthesize the implementation of the `==` operator for structs and enums, eliminating the need for manual implementation.

For enums **without** associated values, the compiler automatically applies `Equatable` conformance and synthesizes the `==` operator without requiring any explicit declaration from the developer.

For structs and enums **with** associated values, the developer must **explicitly declare conformance** to `Equatable` in the original type declaration or in an extension within the same file to trigger synthesis. Additionally, the following conditions must be met:

* **Structs:** All stored properties must conform to `Equatable`.
* **Enums:** All associated values must conform to `Equatable`.

```swift theme={"dark"}
// Implicit conformance and synthesis (no explicit declaration needed)
enum Direction {
    case north, south, east, west
}

// Explicit conformance triggers compiler synthesis
struct Matrix: Equatable {
    let rows: Int
    let columns: Int
    let grid: [Double]
}
```

When synthesis occurs, the compiler generates a memberwise equality check, evaluating `lhs.property == rhs.property` sequentially for all stored properties or associated values.

## Tuple Equality

Tuples in Swift cannot conform to protocols, meaning a tuple can never conform to `Equatable`. However, tuples still support the `==` operator. As of Swift 5.9, the Swift standard library utilizes parameter packs to provide a single variadic generic `==` operator that evaluates tuples of *any* arity. This operator requires that all elements within the tuples are themselves `Equatable` and that both tuples have identical types and structures.

## Value Equality vs. Identity

The `==` operator evaluates **value equality** (semantic equivalence), which is distinct from **reference equality** (identity). For reference types (classes), two distinct instances residing at different memory addresses can evaluate to `true` using `==` if their internal states match the logic defined in their `==` implementation.

To evaluate whether two references point to the exact same memory allocation, Swift provides the `===` (identity) operator, which is tied to the `AnyObject` constraint rather than the `Equatable` protocol.

## Negation

For types conforming to `Equatable`, the `!=` (inequality) operator is automatically provided via a protocol extension. It is implemented by default as the logical negation of the `==` operator. Types that implement `==` without conforming to `Equatable` do not receive this default implementation and must implement `!=` manually if inequality checks are required.

```swift theme={"dark"}
extension Equatable {
    static func != (lhs: Self, rhs: Self) -> Bool {
        return !(lhs == rhs)
    }
}
```

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