# Type '(Int, ())' cannot conform to 'Equatable'

**URL:** <https://forums.swift.org/t/type-int-cannot-conform-to-equatable/69125>\
**Category:** Using Swift\
**Tags:** equatable\
**Created:** [December 22, 2023, 6:47am UTC](https://forums.swift.org/t/type-int-cannot-conform-to-equatable/69125 "2023-12-22T06:47:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [December 22, 2023, 6:47am UTC](https://forums.swift.org/t/type-int-cannot-conform-to-equatable/69125/1 "2023-12-22T06:47:01Z")

</div>

After SE283, we have Equatable support for tuple so that (Int, Int) can conform to `Equatable`.

However since () does not conform to Equatable, we'll still be stuck here.

See real world example from `OpenCombine`

```swift
XCTAssertEqual(history.count, 3)
// tuples aren't Equatable, so matching the elements one by one
switch history[0] {
case .subscription("Zip"):
  break
default:
  XCTFail("Failed to match the first subscription event in \(#function)")
}

switch history[1] {
case .value(let v):
    if v.0 != 42 || v.1 != () {
        XCTFail("Failed to match the value event in \(#function)")
    }
default:
  XCTFail("Failed to match the value event in \(#function)")
}

switch history[2] {
case .completion(.finished):
    break
default:
    XCTFail("Failed to match the completion event in \(#function)")
}

```

If we can make () to conform to Equatable, we can just use very simple compare.

```swift
XCTAssertEqual(history, [.subscription("Zip"), .value(42, ()), .completion(.finished)])

```

---

<div class="post-metadata">

**Author:** ![1-877-547-7272](https://avatars.discourse-cdn.com/v4/letter/1/278dde/32.png) [@1-877-547-7272](https://forums.swift.org/u/1-877-547-7272)\
**Post date:** [December 22, 2023, 7:19am UTC](https://forums.swift.org/t/type-int-cannot-conform-to-equatable/69125/2 "2023-12-22T07:19:43Z")

</div>

I don't believe SE-0283 was ever actually added to the language. There were [issues with the implementation](https://forums.swift.org/t/implementation-issues-with-se-0283-tuples-are-ehc/46946) that led to its [merge being reverted](https://forums.swift.org/t/status-of-se-0283-tuples-conform-to-equatable-comparable-and-hashable/46942), and I believe at this point its acceptance is [considered expired](https://forums.swift.org/t/addressing-unimplemented-evolution-proposals/40322).

There is a more recent pitch for user-definable tuple conformances that would include this in the standard library, though.

> [@Pitch: User-defined tuple conformances](https://forums.swift.org/t/pitch-user-defined-tuple-conformances/67154):
>
> Tuple conformances Authors: [Slava Pestov](https://github.com/slavapestov) Status: Implementation in progress Upcoming Feature Flag: TupleConformances Previous Proposal: SE-0283 Tuples Conform to Equatable, Comparable, and Hashable Review: ([pitch](https://forums.swift.org/...)) Introduction Tuples cannot conform to protocols today, and this surfaces in the form of obvious limitations, such as not being able to use a tuple of Hashable values as a Dictionary key. Motivation The desire for tuples to conform to certain standard library protocols was previously…

According to the post, you can enable the `TupleConformances` upcoming feature flag to try it out. It might not be stable or fully implemented yet, though.

---

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [December 22, 2023, 8:19am UTC](https://forums.swift.org/t/type-int-cannot-conform-to-equatable/69125/3 "2023-12-22T08:19:35Z")

</div>

Oh yeah. I thought it was implemented because we can do "b1 == b2". But actually they are not conforming to `Equatable`. (Confused me: where is the == implementation 🤔？)

```swift
public func test() {
    let a1 = (3, ())
    let a2 = (4, ())

    let b1 = (3, 3)
    let b2 = (4, 3)
    b1 == b2 // ⚠️ Result of operator '==' is unused
    e(b1, b2) // ❌ Argument type '(Int, Int)' does not conform to expected type 'Equatable'
    e(a1, a2) // ❌ Argument type '(Int, ())' does not conform to expected type 'Equatable'
}

func e(_ a: any Equatable, _ b: any Equatable) { ... }

```

> According to the post, you can enable the `TupleConformances` upcoming feature flag to try it out.

It seems that it has not been integrated into main / release/5.10 of Swift. And we need a corresponding PR toolchain to test it.

---

<div class="post-metadata">

**Author:** ![Kyle-Ye](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyle-ye/32/19686_2.png) [@Kyle-Ye](https://forums.swift.org/u/Kyle-Ye)\
**Post date:** [December 22, 2023, 8:23am UTC](https://forums.swift.org/t/type-int-cannot-conform-to-equatable/69125/4 "2023-12-22T08:23:29Z")

</div>

Found this [SE0015](https://github.com/apple/swift-evolution/blob/main/proposals/0015-tuple-comparison-operators.md)

Looks like Swift have hardcoded such comparison ability in the core library. (Supporting tuple count from 2 to 6)

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [December 22, 2023, 2:34pm UTC](https://forums.swift.org/t/type-int-cannot-conform-to-equatable/69125/5 "2023-12-22T14:34:23Z")

</div>

> [@1-877-547-7272](#):
>
> According to the post, you can enable the `TupleConformances` upcoming feature flag to try it out. It might not be stable or fully implemented yet, though.

Experimental feature flags are only available in developer toolchains, and tuple conformances are not implemented all the way yet, in fact most examples will crash once you get past type checking.

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [December 22, 2023, 5:12pm UTC](https://forums.swift.org/t/type-int-cannot-conform-to-equatable/69125/6 "2023-12-22T17:12:14Z")

</div>

> [@Kyle-Ye](#):
>
> Confused me: where is the == implementation 🤔？

Not sure which file exactly, but somewhere in the standard library there are top-level functions `==` and `!=` with overloads for tuples of up to six elements. I believe there’s the other comparison operators too, but I haven’t tested them yet.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [December 22, 2023, 6:35pm UTC](https://forums.swift.org/t/type-int-cannot-conform-to-equatable/69125/7 "2023-12-22T18:35:34Z")

</div>

> [@Kyle-Ye](#):
>
> If we can make () to conform to Equatable, we can just use very simple compare.
> 
> ```swift
> XCTAssertEqual(history, [.subscription("Zip"), .value(42, ()), .completion(.finished)])
> 
> ```

You could still do that if you conform the embedding type to Equatable, right? Something like that:

```swift
extension HistoryElement: Equatable {
    static func == (lhs: Self, rhs: Self) -> Bool {
        switch (lhs, rhs) {
            case let (.subscription(a), .subscription(b)): a == b
            case let (.completion(a), .completion(b)): a == b
            case let (.value(a, b), .value(c, d)): a == c && b == d
            default: false
        }
    }
}

```
