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

Martin R martinr448 at gmail.com
Sun Jul 9 14:14:38 CDT 2017


> On 9. Jul 2017, at 21:00, Jens Persson via swift-users <swift-users at swift.org> wrote:
> 
> Since Array has .elementsEqual, another workaround (until conditional conformance) is:
> 
> class Tree : Equatable {
>     let rootData:Int
>     let children:[(String, Tree)]
>     
>     init(rootData: Int, children: [(String, Tree)]) {
>         self.rootData = rootData
>         self.children = children
>     }
>     static public func ==(_ lhs:Tree, _ rhs:Tree) -> Bool {
>         return lhs.rootData == rhs.rootData &&
>             lhs.children.elementsEqual(rhs.children, by: { (a: (String, Tree), b: (String, Tree)) -> Bool in
>                 return a.0 == b.0 && a.1 == b.1
>             })
>     }
> }


Slightly simpler (since == is already defined for the tuples):

    class Tree : Equatable {
        let rootData:Int = 0
        let children:[(String, Tree)] = []
        
        static public func ==(_ lhs:Tree, _ rhs:Tree) -> Bool {
            return lhs.rootData == rhs.rootData &&
                lhs.children.elementsEqual(rhs.children, by: ==)
        }
    }


> 
> 
> On Sun, Jul 9, 2017 at 8:44 PM, David Sweeris <davesweeris at mac.com <mailto:davesweeris at mac.com>> wrote:
> 
> On Jul 9, 2017, at 10:06, David Baraff via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
> 
>> 
>>> On Jul 9, 2017, at 8:27 AM, Jens Persson <jens at bitcycle.com <mailto:jens at bitcycle.com>> wrote:
>>> 
>>> (Also, note that your implementation of == uses lhs === rhs thus will only return true when lhs and rhs are the same instance of SomeClass.)
>> Of course — i threw that in just to make a simple example.
>> 
>> Followup question: what I really wanted to write was an == operator for a tree:
>> 
>> // silly tree, useful for nothing
>> class Tree : Equatable {
>>    let rootData:Int
>>    let children:[(String, Tree)]
>> 
>>    static public func ==(_ lhs:Tree, _ rhs:Tree) {
>> 	return lhs.rootData == rhs.rootData && 
>>             lhs.children == rhs.children		// sadly, this doesn’t compile
>>    }
>> }
> 
> Right, the `==` func is *defined* for 2-element tuples where both elements conform to `Equatable`, but that tuple type doesn't itself *conform* to `Equatable`. So the`==` func that's defined on "Array where Element: Equatable" can't see it.
> 
> We'd need both "conditional conformance" and "tuple conformance" in order for that to Just Work.
> 
> - Dave Sweeris 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org <mailto:swift-users at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-users <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/33ef7f5c/attachment.html>


More information about the swift-users mailing list