> On Jul 9, 2017, at 8:27 AM, Jens Persson <[email protected]> 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
}
}
I.e. since now that tuples of (Int, Tree) can be compared, I thought, great, I
get an incredibly elegant description: just compare the child arrays. But this
doesn’t compile, and I assume that telling the compiler that (T,Tree) is a
tuple that conforms to Equatable is just not happening in this lifetime?
Not that it’s a big deal: I can of course write
static public func ==(_ lhs:Tree, _ rhs:Tree) {
return lhs.rootData == rhs.rootData && lhs.children.count ==
rhs.children.count &&
all(lhs.children.indices.map { lhs.children[$0] == rhs.children[$0] }
}
But still: it would be nice to not have to break it down manually. (all() is a
free function doing what you would guess it does. i gave up resisting the urge
and defined both any() and all(), after years of loving them in Python. I know
I should just a more swift-like idiom, but dang it, it’s just too short. I
would really love for any() and all() to become standard lib free functions.
they’re so incredibly useful.)
> /Jens
>
> On Sun, Jul 9, 2017 at 5:24 PM, Jens Persson <[email protected]
> <mailto:[email protected]>> wrote:
> Making SomeClass conform to Equatable should fix it:
> class SomeClass : Equatable {
> static public func ==(_ lhs:SomeClass, _ rhs:SomeClass) -> Bool {
> return lhs === rhs
> }
> }
> /Jens
>
>
> On Sun, Jul 9, 2017 at 5:11 PM, David Baraff via swift-users
> <[email protected] <mailto:[email protected]>> 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
> [email protected] <mailto:[email protected]>
> https://lists.swift.org/mailman/listinfo/swift-users
> <https://lists.swift.org/mailman/listinfo/swift-users>
>
>
>
_______________________________________________
swift-users mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-users