<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 15, 2017 at 6:58 PM, Joe Groff via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">SE-0185 is awesome, and brings the long-awaited ability for the compiler to provide a default implementation of `==` and `hashValue` when you don't provide one yourself. Doug and I were talking the other day and thought of a potential pitfall: what should happen if you provide a manual implementation of `==` without also manually writing your own `hashValue`? It's highly likely that the default implementation of `hashValue` will be inconsistent with `==` and therefore invalid in a situation like this:<br>
<br>
struct Foo: Hashable {<br>
// This property is "part of the value"<br>
var involvedInEquality: Int<br>
// This property isn't; maybe it's a cache or something like that<br>
var notInvolvedInEquality: Int<br>
<br>
static func ==(a: Foo, b: Foo) -> Bool {<br>
return a.involvedInEquality == b.involvedInEquality<br>
}<br>
}<br>
<br>
As currently implemented, the compiler will still give `Foo` the default hashValue implementation, which will use both of `Foo`'s properties to compute the hash, even though `==` only tests one. This could be potentially dangerous. Should we suppress the default hashValue derivation when an explicit == implementation is provided?<br></blockquote><div><br></div><div>I would agree with the other people saying automatic implementation of Hashable should be all-or-nothing, but this makes me wonder : shouldn't this "all-or-nothign" rule be the same for all the automatically implemented protocol ? If no, then how would you know, just by looking at the protocol, which function is "overridable" and which isn't ? I suppose trying and having the compiler raise an error shouldn't be the only option.<br></div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
-Joe<br>
______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
</blockquote></div><br></div></div>