[swift-users] is this a defect in equatable for swift tuples?

Martin R martinr448 at gmail.com
Sun Jul 9 10:33:07 CDT 2017


The == operator is defined for tuples with (up to 6) elements that conform to the Equatable protocol
(and < for tuples with Comparable elements):

    class SomeClass: Equatable {
        static public func ==(_ lhs:SomeClass, _ rhs:SomeClass) -> Bool {
            return lhs === rhs
        }
    }

    let c1 = SomeClass()
    let c2 = SomeClass()

    let t1 = ("abc", c1)
    let t2 = ("abc", c2)

    c1 == c2        // legal
    t1 == t2        // legal

The existence of a == operator alone, without declaring protocol conformance, is not sufficient.


> On 9. Jul 2017, at 17:11, David Baraff via swift-users <swift-users at swift.org> wrote:
> 
> Given 2-tuples of type (T1, T2), you should be able to invoke the == operator if you could on both types T1 and T2, right?  i.e.
> 
> (“abc”, 3) == (“abc”, 4)	// legal
> 
> but:
> 
> class SomeClass {
>     static public func ==(_ lhs:SomeClass, _ rhs:SomeClass) -> Bool {
>         return lhs === rhs
>     }
> }
> 
> let c1 = SomeClass()
> let c2 = SomeClass()
> 
> let t1 = ("abc", c1)
> let t2 = ("abc", c2)
> 
> c1 == c2		// legal
> t1 == t2		// illegal
> 
> 
> 
> 
> Why is t1 == t2 not legal given that c1 == c2 IS legal?
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170709/314a2856/attachment.html>


More information about the swift-users mailing list