On Thu, Aug 10, 2017 at 11:05 AM Jordan Rose <jordan_r...@apple.com> wrote:

> [Proposal:
> https://github.com/apple/swift-evolution/blob/master/proposals/0185-synthesize-equatable-hashable.md
> ]
>
> Hi, Tony. Glad to see this back again!
>
> Overall I'm an enthusiastic +1. The restrictions and future work you've
> listed make sense, and I think this is the right starting place. I just
> have one thing I'd want to clarify:
>
> Any user-provided implementations of == or hashValue will override the
> default implementations that would be provided by the compiler.
>
>
> Does this include implementations in (possibly constrained) protocol
> extensions? I assume yes, but that's probably worth calling out explicitly.
> Still, it could be confusing to some users.
>

Yes, manual implementations added in extensions override the
compiler-synthesized default:

Without constraints:
(swift) struct Foo: Equatable { let x: Int }
(swift) Foo(x: 5) == Foo(x: 6)
// r0 : Bool = false
(swift) Foo(x: 5) == Foo(x: 5)
// r1 : Bool = true
(swift) extension Foo { static func ==(lhs: Foo, rhs: Foo) -> Bool { return
lhs.x % 2 == rhs.x % 2 } }
(swift) Foo(x: 5) == Foo(x: 6)
// r2 : Bool = false
(swift) Foo(x: 5) == Foo(x: 7)
// r3 : Bool = true

With constraints:
(swift) struct Foo<T: Equatable>: Equatable { let t: T }
(swift) extension Foo where T == String { static func ==(lhs: Foo<T>, rhs:
Foo<T>) -> Bool { return lhs.t.characters.count == rhs.t.characters.count }
}
(swift) Foo(t: "foo") == Foo(t: "bar")
// r0 : Bool = true
(swift) Foo(t: 5) == Foo(t: 7)
// r1 : Bool = false

I can update the text to make this explicit.



>
> Jordan
>
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to