<div dir="ltr">I am +1 on being able to opt in for a derived `hashValue` and an emphatic +1 to the idea of being able to make functions conform to `Equatable` and `Hashable` at all. I agree with the sentiment that it should still be opt in, however. <div><br></div><div>TJ</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 9, 2016 at 8:51 AM, Step C 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"><div dir="auto"><div><span></span></div><div><div>I find it valuable to think explicitly about what equality means for my types, though not so valuable to write a bunch of boilerplate to support it. Derivable or automatic conformances might also carry us further towards users being able to provide their own automagic conformances. </div><span class=""><div><br></div><div><blockquote type="cite"><span style="background-color:rgba(255,255,255,0)">- Class references can be considered equal if they refer to the same instance,</span></blockquote></div></span><div>With opt-in conformance we could potentially have field comparison for classes too. I guess that would still need to be a customization point regardless as neither approach is always the right answer. </div><div><div class="h5"><div><br>On Mar 9, 2016, at 6:29 AM, Haravikk via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><div>While I appreciate the idea behind the proposal, I think I’m a -1 to it. Java has required equality and hashable as part of its base Object class, but I frequently encountered classes that had very poor implementations for these, or never bothered to provide one; arguably they didn’t need to, which is fine, but it kind of went against the whole idea.</div><div><br></div><div>Swift has some pretty nifty features that also make this redundant, for example, I’ve been working on some ordered collection types; my natural inclination was to require that values be Comparable, however this actually limits the usefulness of the collection (or requires values to be wrapped somehow). Instead I decided to accept values of any type, and also take a closure (same as used to sort an array).</div><div><br></div><div>However, with generic constraints I can still provide a default closure for Comparable types like so:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><font face="Monaco">// Sort Comparable elements in ascending order if no closure is provided.</font></div><div><font face="Monaco">extension OrderedCollection where Self.Generator.Element:Comparable {</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>init<S:SequenceType where S.Generator.Element == Generator.Element>(elements:S) {</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">                </span>self.init(isOrderedBefore: { $0 < $1 }, elements: elements)</font></div><div><font face="Monaco"><span style="white-space:pre-wrap">        </span>}</font></div><div><font face="Monaco">}</font></div></blockquote><div><br></div><div>(the same feature also lets me implement ArrayLiteralConvertible for Comparable arrays, though I have to provide a default initialiser producing a fatal error for the rest)</div><div><br></div><div><br></div><div>It’s a bit of a weird thing to get your head around at first, but you can solve a lot of problems in a similar way, without having to place overly strict requirements on the types that you can accept, removing the need for all types to conform to anything.</div><br><div><blockquote type="cite"><div>On 9 Mar 2016, at 08:30, Austin Zheng via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><br><div><div style="word-wrap:break-word">As Brent pointed out, adding this sort of support opens a whole can of worms. Large parts of the standard library would silently become unsound.<div><br></div><div>As well, in my experience people who have had trouble using (e.g.) Equatable with heterogeneous collections are often trying to do type-unsound things. Maybe Swift should support a separate notion of heterogenous equality for comparisons between Equatable types (and one of the POP WWDC talks actually sketched out an outline of how this might be done), but that's different from making Equatable universal. In addition, I think Swift 3's proposed support for conditional protocol conformance will make creating principled heterogeneous collections easier, which should ease some of the burden.<div><br></div><div>Best,</div><div>Austin<br><div><br><div><blockquote type="cite"><div>On Mar 9, 2016, at 12:17 AM, David Hart <<a href="mailto:david@hartbit.com" target="_blank">david@hartbit.com</a>> wrote:</div><br><div><div dir="auto"><div><br></div><div>On 08 Mar 2016, at 23:15, Austin Zheng via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">I would prefer Equatable and Hashable to remain opt-in, and for us to add better support for automatic deriving of implementation.</div></div></blockquote><br><div><div><font><span style="background-color:rgba(255,255,255,0)">On 08 Mar 2016, at 23:57, Zach Waldowski via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br><br></span></font></div><blockquote type="cite"><div><font><span style="background-color:rgba(255,255,255,0)">I completely agree with Austin here. Automatic derivation (perhaps through the same mechanisms Joe is talking about) would be a nice enhancement, but I find it refreshing and advantageous for simple value types to have very little automatic behavior.</span></font></div></blockquote><br></div><div>Pedantically I agree with both of you, but from a very pragmatic point of you, I think it's very important to point out what Joe said about how this could reduce one of the most frustrating aspects of Swift, when people work with heterogeneous arrays and try to conform to Equatable:</div><div><br></div><div><blockquote type="cite"><span style="background-color:rgba(255,255,255,0)">that would solve many of the common problems people currently have trying to work with heterogeneous containers.</span></blockquote></div><div><br></div><div><br></div></div></div></blockquote></div><br></div></div></div></div>_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></div></div></div><br>_______________________________________________<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/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>