<div dir="ltr">Thanks so much for putting this together, Tony! Glad I was able to be some inspiration. :^)<br><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, May 25, 2016 at 1:28 PM, Tony Allevato via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I was inspired to put together a draft proposal based on an older discussion in the Universal Equality, Hashability, and Comparability thread &lt;<a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/8919/" target="_blank">http://thread.gmane.org/gmane.comp.lang.swift.evolution/8919/</a>&gt; that recently got necromanced (thanks Mark Sands!).<div><br></div><div>I&#39;m guessing that this would be a significant enough change that it&#39;s not possible for the Swift 3 timeline, but it&#39;s something that would benefit enough people that I want to make sure the discussion stays alive. If there are enough good feelings about it, I&#39;ll move it from my gist into an actual proposal PR.</div><div><br></div><div><h1 style="font-size:2.25em;margin:0px 0px 16px;line-height:1.2;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;">Automatically deriving<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code><span> </span>and<code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:inherit;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code><span> </span>for value types</h1><ul style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px"><li>Proposal:<span> </span><a href="https://github.com/apple/swift-evolution/blob/master/proposals/NNNN-name.md" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">SE-0000</a></li><li>Author(s):<span> </span><a href="https://github.com/allevato" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">Tony Allevato</a></li><li>Status:<span> </span><span style="font-weight:bolder"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#rationale" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">Awaiting review</a></span></li><li>Review manager: TBD</li></ul><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#introduction" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"></a>Introduction</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Value types are prevalent throughout the Swift language, and we encourage developers to think in those terms when writing their own types. Frequently, developers find themselves writing large amounts of boilerplate code to support equatability and hashability of value types. This proposal offers a way for the compiler to automatically derive conformance to<code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code><span> </span>and<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code><span> </span>to reduce this boilerplate, in a subset of scenarios where generating the correct implementation is likely to be possible.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Swift-evolution thread:<span> </span><a href="http://thread.gmane.org/gmane.comp.lang.swift.evolution/8919" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">Universal Equatability, Hashability, and Comparability</a></p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#motivation" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"></a>Motivation</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Building robust value types in Swift can involve writing significant boilerplate code to support concepts of hashability and equatability. Equality is pervasive across many value types, and for each one users must implement the<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">==</code><span> </span>operator such that it performs a fairly rote memberwise equality test. As an example, an equality test for a struct looks fairly uninteresting:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px"><pre style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;border-radius:3px;word-break:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">==</span>(lhs: Foo, rhs: Foo) <span style="color:rgb(167,29,93)">-&gt;</span> <span style="color:rgb(0,134,179)">Bool</span> {
  <span style="color:rgb(167,29,93)">return</span> lhs<span style="color:rgb(167,29,93)">.</span>property1 <span style="color:rgb(167,29,93)">==</span> rhs<span style="color:rgb(167,29,93)">.</span>property1 <span style="color:rgb(167,29,93)">&amp;&amp;</span>
         lhs<span style="color:rgb(167,29,93)">.</span>property2 <span style="color:rgb(167,29,93)">==</span> rhs<span style="color:rgb(167,29,93)">.</span>property2 <span style="color:rgb(167,29,93)">&amp;&amp;</span>
         lhs<span style="color:rgb(167,29,93)">.</span>property3 <span style="color:rgb(167,29,93)">==</span> rhs<span style="color:rgb(167,29,93)">.</span>property3 <span style="color:rgb(167,29,93)">&amp;&amp;</span>
         <span style="color:rgb(167,29,93)">...</span>
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">What&#39;s worse is that this operator must be updated if any properties are added, removed, or changed, and since it must be manually written, it&#39;s possible to get it wrong, either by omission or typographical error.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Likewise, hashability is necessary when one wishes to store a value type in a<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Set</code><span> </span>or use one as a multi-valued<code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Dictionary</code><span> </span>key. Writing high-quality, well-distributed hash functions is not trivial so developers may not put a great deal of thought into them – especially as the number of properties increases – not realizing that their performance could potentially suffer as a result. And as with equality, writing it manually means there is the potential to get it wrong.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">In particular, the code that must be written to implement equality for enums is quite verbose. One such real-world example (<a href="https://github.com/exercism/xswift/blob/master/exercises/poker/PokerExample.swift#L151-L189" style="color:rgb(64,120,192);text-decoration:none;background-color:transparent" target="_blank">source</a>):</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px"><pre style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;border-radius:3px;word-break:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">==</span>(lhs: HandRank, rhs: HandRank) <span style="color:rgb(167,29,93)">-&gt;</span> <span style="color:rgb(0,134,179)">Bool</span> {
  <span style="color:rgb(167,29,93)">switch</span> (lhs, rhs) {
  <span style="color:rgb(167,29,93)">case</span> (<span style="color:rgb(167,29,93)">.</span>straightFlush(<span style="color:rgb(167,29,93)">let</span> lRank, <span style="color:rgb(167,29,93)">let</span> lSuit), <span style="color:rgb(167,29,93)">.</span>straightFlush(<span style="color:rgb(167,29,93)">let</span> rRank , <span style="color:rgb(167,29,93)">let</span> rSuit)):
    <span style="color:rgb(167,29,93)">return</span> lRank <span style="color:rgb(167,29,93)">==</span> rRank <span style="color:rgb(167,29,93)">&amp;&amp;</span> lSuit <span style="color:rgb(167,29,93)">==</span> rSuit
  <span style="color:rgb(167,29,93)">case</span> (<span style="color:rgb(167,29,93)">.</span>fourOfAKind(four: <span style="color:rgb(167,29,93)">let</span> lFour), <span style="color:rgb(167,29,93)">.</span>fourOfAKind(four: <span style="color:rgb(167,29,93)">let</span> rFour)):
    <span style="color:rgb(167,29,93)">return</span> lFour <span style="color:rgb(167,29,93)">==</span> rFour
  <span style="color:rgb(167,29,93)">case</span> (<span style="color:rgb(167,29,93)">.</span>fullHouse(three: <span style="color:rgb(167,29,93)">let</span> lThree), <span style="color:rgb(167,29,93)">.</span>fullHouse(three: <span style="color:rgb(167,29,93)">let</span> rThree)):
    <span style="color:rgb(167,29,93)">return</span> lThree <span style="color:rgb(167,29,93)">==</span> rThree
  <span style="color:rgb(167,29,93)">case</span> (<span style="color:rgb(167,29,93)">.</span>flush(<span style="color:rgb(167,29,93)">let</span> lRank, <span style="color:rgb(167,29,93)">let</span> lSuit), <span style="color:rgb(167,29,93)">.</span>flush(<span style="color:rgb(167,29,93)">let</span> rRank, <span style="color:rgb(167,29,93)">let</span> rSuit)):
    <span style="color:rgb(167,29,93)">return</span> lSuit <span style="color:rgb(167,29,93)">==</span> rSuit <span style="color:rgb(167,29,93)">&amp;&amp;</span> lRank <span style="color:rgb(167,29,93)">==</span> rRank
  <span style="color:rgb(167,29,93)">case</span> (<span style="color:rgb(167,29,93)">.</span>straight(high: <span style="color:rgb(167,29,93)">let</span> lRank), <span style="color:rgb(167,29,93)">.</span>straight(high: <span style="color:rgb(167,29,93)">let</span> rRank)):
    <span style="color:rgb(167,29,93)">return</span> lRank <span style="color:rgb(167,29,93)">==</span> rRank
  <span style="color:rgb(167,29,93)">case</span> (<span style="color:rgb(167,29,93)">.</span>threeOfAKind(three: <span style="color:rgb(167,29,93)">let</span> lRank), <span style="color:rgb(167,29,93)">.</span>threeOfAKind(three: <span style="color:rgb(167,29,93)">let</span> rRank)):
    <span style="color:rgb(167,29,93)">return</span> lRank <span style="color:rgb(167,29,93)">==</span> rRank
  <span style="color:rgb(167,29,93)">case</span> (<span style="color:rgb(167,29,93)">.</span>twoPair(high: <span style="color:rgb(167,29,93)">let</span> lHigh, low: <span style="color:rgb(167,29,93)">let</span> lLow, highCard: <span style="color:rgb(167,29,93)">let</span> lCard),
        <span style="color:rgb(167,29,93)">.</span>twoPair(high: <span style="color:rgb(167,29,93)">let</span> rHigh, low: <span style="color:rgb(167,29,93)">let</span> rLow, highCard: <span style="color:rgb(167,29,93)">let</span> rCard)):
    <span style="color:rgb(167,29,93)">return</span> lHigh <span style="color:rgb(167,29,93)">==</span> rHigh <span style="color:rgb(167,29,93)">&amp;&amp;</span> lLow <span style="color:rgb(167,29,93)">==</span> rLow <span style="color:rgb(167,29,93)">&amp;&amp;</span> lCard <span style="color:rgb(167,29,93)">==</span> rCard
  <span style="color:rgb(167,29,93)">case</span> (<span style="color:rgb(167,29,93)">.</span>onePair(<span style="color:rgb(167,29,93)">let</span> lPairRank, card1: <span style="color:rgb(167,29,93)">let</span> lCard1, card2: <span style="color:rgb(167,29,93)">let</span> lCard2, card3: <span style="color:rgb(167,29,93)">let</span> lCard3),
        <span style="color:rgb(167,29,93)">.</span>onePair(<span style="color:rgb(167,29,93)">let</span> rPairRank, card1: <span style="color:rgb(167,29,93)">let</span> rCard1, card2: <span style="color:rgb(167,29,93)">let</span> rCard2, card3: <span style="color:rgb(167,29,93)">let</span> rCard3)):
    <span style="color:rgb(167,29,93)">return</span> lPairRank <span style="color:rgb(167,29,93)">==</span> rPairRank <span style="color:rgb(167,29,93)">&amp;&amp;</span> lCard1 <span style="color:rgb(167,29,93)">==</span> rCard1 <span style="color:rgb(167,29,93)">&amp;&amp;</span> lCard2 <span style="color:rgb(167,29,93)">==</span> rCard2 <span style="color:rgb(167,29,93)">&amp;&amp;</span> lCard3 <span style="color:rgb(167,29,93)">==</span> rCard3
  <span style="color:rgb(167,29,93)">case</span> (<span style="color:rgb(167,29,93)">.</span>highCard(<span style="color:rgb(167,29,93)">let</span> lCard), <span style="color:rgb(167,29,93)">.</span>highCard(<span style="color:rgb(167,29,93)">let</span> rCard)):
    <span style="color:rgb(167,29,93)">return</span> lCard <span style="color:rgb(167,29,93)">==</span> rCard
  <span style="color:rgb(167,29,93)">default</span>:
    <span style="color:rgb(167,29,93)">return</span> <span style="color:rgb(0,134,179)">false</span>
  }
}</pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Crafting a high-quality hash function for this enum would be similarly inconvenient to write, involving another large<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">switch</code>statement.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Swift already provides implicit protocol conformance in some cases; notably, enums with raw values conform to<code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">RawRepresentable</code>,<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code>, and<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code><span> </span>without the user explicitly declaring them:</p><div style="margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px"><pre style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;margin-top:0px;margin-bottom:0px;font-stretch:normal;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;border-radius:3px;word-break:normal;background-color:rgb(247,247,247)"><span style="color:rgb(167,29,93)">enum</span> Foo: <span style="color:rgb(0,134,179)">Int</span> {
  <span style="color:rgb(167,29,93)">case</span> one <span style="color:rgb(167,29,93)">=</span> <span style="color:rgb(0,134,179)">1</span>
  <span style="color:rgb(167,29,93)">case</span> two <span style="color:rgb(167,29,93)">=</span> <span style="color:rgb(0,134,179)">2</span>
}

<span style="color:rgb(167,29,93)">let</span> x <span style="color:rgb(167,29,93)">=</span> (Foo<span style="color:rgb(167,29,93)">.</span>one <span style="color:rgb(167,29,93)">==</span> Foo<span style="color:rgb(167,29,93)">.</span>two)  <span style="color:rgb(150,152,150)">// works</span>
<span style="color:rgb(167,29,93)">let</span> y <span style="color:rgb(167,29,93)">=</span> Foo<span style="color:rgb(167,29,93)">.</span>one<span style="color:rgb(167,29,93)">.</span><span style="color:rgb(0,134,179)">hashValue</span>     <span style="color:rgb(150,152,150)">// also works</span>
<span style="color:rgb(167,29,93)">let</span> z <span style="color:rgb(167,29,93)">=</span> Foo<span style="color:rgb(167,29,93)">.</span>one<span style="color:rgb(167,29,93)">.</span><span style="color:rgb(0,134,179)">rawValue</span>      <span style="color:rgb(150,152,150)">// also also works</span></pre></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Since there is precedent for this in Swift, we propose extending this support to more value types.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#proposed-solution" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"></a>Proposed solution</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">We propose that a value type be<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code>/<code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code><span> </span>if all of its members are<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code>/<code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code>, with the result for the outer type being composed from its members.</p><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Specifically, we propose the following rules for deriving<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code>:</p><ul style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px"><li><p style="margin-top:16px;margin-bottom:16px">A<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">struct</code><span> </span>implicitly conforms to<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code><span> </span>if all of its fields are of types that conform to<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code><span> </span>– either explicitly, or implicitly by the application of these rules. The compiler will generate an implementation of<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">==(lhs: T, rhs: T)</code>that returns true if and only if<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">lhs.x == rhs.x</code><span> </span>for all fields<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">x</code><span> </span>in<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">T</code>.</p></li><li><p style="margin-top:16px;margin-bottom:16px">An<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">enum</code><span> </span>implicitly conforms to<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code><span> </span>if all of its associated values across all of its cases are of types that conform to<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code><span> </span>– either explicitly, or implicitly by the application of these rules. The compiler will generate an implementation of<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">==(lhs: T, rhs: T)</code><span> </span>that returns true if and only if<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">lhs</code><span> </span>and<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">rhs</code><span> </span>are the same case and have payloads that are memberwise-equal.</p></li></ul><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Likewise, we propose the following rules for deriving<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code>:</p><ul style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px"><li><p style="margin-top:16px;margin-bottom:16px">A<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">struct</code><span> </span>implicitly conforms to<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code><span> </span>if all of its fields are of types that conform to<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code><span> </span>– either explicitly, or implicitly by the application of these rules. The compiler will generate an implementation of<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">hashValue</code><span> </span>that uses a pre-defined hash function<span style="font-size:12px;line-height:0;vertical-align:baseline">†</span><span> </span>to compute the hash value of the struct from the hash values of its members.</p><p style="margin-top:16px;margin-bottom:16px">Since order of the terms affects the hash value computation, we recommend ordering the terms in member definition order.</p></li><li><p style="margin-top:16px;margin-bottom:16px">An<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">enum</code><span> </span>implicitly conforms to<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code><span> </span>if all of its associated values across all of its cases are of types that conform to<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code><span> </span>– either explicitly, or implicitly by the application of these rules. The compiler will generate an implementation of<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">hashValue</code><span> </span>that uses a pre-defined hash function<span style="font-size:12px;line-height:0;vertical-align:baseline">†</span><span> </span>to compute the hash value of an enum value by using the case&#39;s ordinal (i.e., definition order) followed by the hash values of its associated values as its terms, also in definition order.</p></li></ul><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px"><span style="font-size:12px;line-height:0;vertical-align:baseline">†</span><span> </span>We leave the exact definition of the hash function unspecified here; a multiplicative hash function such as Kernighan and Ritchie or Bernstein is easy to implement, but we do not rule out other possibilities.</p><h3 style="margin-top:1em;margin-bottom:16px;line-height:1.43;font-size:1.5em;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#overriding-defaults" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1.2;background-color:transparent" target="_blank"></a>Overriding defaults</h3><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Any user-provided implementations of<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">==</code><span> </span>or<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">hashValue</code><span> </span>should override the default implementations that would be provided by the compiler. This is already possible today with raw-value enums so the same behavior should be extended to other value types that are made to implicitly conform to these protocols.</p><h3 style="margin-top:1em;margin-bottom:16px;line-height:1.43;font-size:1.5em;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#open-questions" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1.2;background-color:transparent" target="_blank"></a>Open questions</h3><h4 style="margin-top:1em;margin-bottom:16px;line-height:1.4;font-size:1.25em;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#omission-of-fields-from-generated-computations" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1.2;background-color:transparent" target="_blank"></a>Omission of fields from generated computations</h4><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Should it be possible to easily omit certain properties from automatically generated equality tests or hash value computation? This could be valuable, for example, if a property is merely used as an internal cache and does not actually contribute to the &quot;value&quot; of the instance. Under the rules above, if this cached value was equatable, a user would have to override<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">==</code><span> </span>and<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">hashValue</code><span> </span>and provide their own implementations to ignore it. If there is significant evidence that this pattern is common and useful, we could consider adding a custom attribute, such as<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">@transient</code>, that would omit the property from the generated computations.</p><h4 style="margin-top:1em;margin-bottom:16px;line-height:1.4;font-size:1.25em;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#explicit-or-implicit-derivation" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1.2;background-color:transparent" target="_blank"></a>Explicit or implicit derivation</h4><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">As with raw-value enums today, should the derived conformance be completely explicit, or should users have to explicitly list conformance with<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Equatable</code><span> </span>and<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Hashable</code><span> </span>in order for the compiler to generate the derived implementation?</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#impact-on-existing-code" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"></a>Impact on existing code</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">This change will have no impact on existing code because it is purely additive. Value types that already provide custom implementations of<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">==</code><span> </span>or<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">hashValue</code><span> </span>but satisfy the rules above would keep the custom implementation because it would override the compiler-provided default.</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#alternatives-considered" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"></a>Alternatives considered</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">The original discussion thread also included<span> </span><code style="font-family:consolas,&#39;liberation mono&#39;,menlo,courier,monospace;font-size:13.6px;padding:0.2em 0px;margin:0px;border-radius:3px;background-color:rgba(0,0,0,0.0392157)">Comparable</code><span> </span>as a candidate for automatic generation. Unlike equatability and hashability, however, comparability requires an ordering among the members being compared. Automatically using the definition order here might be too surprising for users, but worse, it also means that reordering properties in the source code changes the code&#39;s behavior at runtime. (This is true for hashability as well if a multiplicative hash function is used, but hash values are not intended to be persistent and reordering the terms does not produce a significant<span> </span><em>behavioral</em><span> </span>change.)</p><h2 style="margin-top:1em;margin-bottom:16px;line-height:1.225;font-size:1.75em;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(238,238,238);color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;"><a href="https://gist.github.com/allevato/2fd10290bfa84accfbe977d8ac07daad#acknowledgments" style="color:rgb(64,120,192);text-decoration:none;display:inline-block;padding-right:2px;line-height:1;background-color:transparent" target="_blank"></a>Acknowledgments</h2><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;helvetica neue&#39;,helvetica,&#39;segoe ui&#39;,arial,freesans,sans-serif,&#39;apple color emoji&#39;,&#39;segoe ui emoji&#39;,&#39;segoe ui symbol&#39;;font-size:16px;line-height:25.6px">Thanks to Joe Groff for spinning off the original discussion thread, Jose Cheyo Jimenez for providing great real-world examples of boilerplate needed to support equatability for some value types, and to Mark Sands for necromancing the swift-evolution thread that convinced me to write this up.</p></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>