<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="georgia, serif">`<span style="color:rgb(51,51,51)">A hash value, provided by a type’s </span><code class="gmail-code-voice" style="box-sizing:inherit;color:rgb(102,102,102)">hashValue</code><span style="color:rgb(51,51,51)"> property, is an integer that is the same for any two instances that compare equally. That is, for two instances </span><code class="gmail-code-voice" style="box-sizing:inherit;color:rgb(102,102,102)">a <div class="gmail_default" style="font-family:georgia,serif;display:inline">​​</div></code><span style="color:rgb(51,51,51)">and </span><code class="gmail-code-voice" style="box-sizing:inherit;color:rgb(102,102,102)">b</code><span style="color:rgb(51,51,51)"> of the same type`</span></font></blockquote><div class="gmail_default"><span style="color:rgb(51,51,51)"><font face="georgia, serif"><br></font></span></div><div class="gmail_default"><font color="#333333" face="georgia, serif">So do you believe A and B are the same type if B inherits A. That&#39;s the differences between you and me. </font></div><div class="gmail_default"><font color="#333333" face="georgia, serif"><br></font></div><div class="gmail_default"><font color="#333333" face="georgia, serif">Zhaoxin</font></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 2, 2016 at 6:55 AM, Jordan Rose <span dir="ltr">&lt;<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div class="h5"><br><div><blockquote type="cite"><div>On Sep 1, 2016, at 15:44, Zhao Xin via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br><div><div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Hi Nick,</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Glad to help.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">but when using third party classes I don’t know if the hash values are comparable<br></blockquote><div><br></div><div class="gmail_default" style="font-family:georgia,serif">You can create an extension with a convenient init(:), which creates a new instance of  the super class basing on the instance of the sub class. That will guarantee the subtraction. Below code works in Xcode 7.3.1 with Swift 2.2.<br></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default"><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">import</span><span> Foundation</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span></span><br></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">func</span><span> ==(lhs: </span><span style="color:rgb(79,129,135)">Foo</span><span>, rhs: </span><span style="color:rgb(79,129,135)">Foo</span><span>) -&gt; </span><span style="color:rgb(112,61,170)">Bool</span><span> {</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>    </span><span style="color:rgb(187,44,162)">return</span><span> lhs.</span><span style="color:rgb(79,129,135)">id</span><span> == rhs.</span><span style="color:rgb(79,129,135)">id</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>}</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span></span><br></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(112,61,170)"><span style="color:rgb(187,44,162)">class</span><span> Foo:</span><span>Hashable</span><span> {</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>    </span><span style="color:rgb(187,44,162)">let</span><span> id:</span><span style="color:rgb(112,61,170)">Int</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>    </span><span style="color:rgb(187,44,162)">var</span><span> hashValue: </span><span style="color:rgb(112,61,170)">Int</span><span> {</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>        </span><span style="color:rgb(187,44,162)">return</span><span> </span><span style="color:rgb(79,129,135)">id</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>    }</span></div><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span>    </span><br></p><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>    </span><span style="color:rgb(187,44,162)">required</span><span> </span><span style="color:rgb(187,44,162)">init</span><span>(</span><span style="color:rgb(187,44,162)">_</span><span> id:</span><span style="color:rgb(112,61,170)">Int</span><span>) {</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>        </span><span style="color:rgb(187,44,162)">self</span><span>.</span><span style="color:rgb(79,129,135)">id</span><span> = id</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>    }</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>}</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span></span><br></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">class</span><span> Bar:</span><span style="color:rgb(79,129,135)">Foo</span><span> {</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>    </span><span style="color:rgb(187,44,162)">override</span><span> </span><span style="color:rgb(187,44,162)">var</span><span> hashValue: </span><span style="color:rgb(112,61,170)">Int</span><span> {</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>        </span><span style="color:rgb(187,44,162)">return</span><span> </span><span style="color:rgb(79,129,135)">id</span><span> * </span><span style="color:rgb(39,42,216)">5</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>    }</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span>}</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span></span><br></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">var</span><span> fooSet:</span><span style="color:rgb(112,61,170)">Set</span><span>&lt;</span><span style="color:rgb(79,129,135)">Foo</span><span>&gt; = [</span><span style="color:rgb(79,129,135)">Foo</span><span>(</span><span style="color:rgb(39,42,216)">10</span><span>), </span><span style="color:rgb(79,129,135)">Foo</span><span>(</span><span style="color:rgb(39,42,216)">9</span><span>), </span><span style="color:rgb(79,129,135)">Foo</span><span>(</span><span style="color:rgb(39,42,216)">8</span><span>), </span><span style="color:rgb(79,129,135)">Foo</span><span>(</span><span style="color:rgb(39,42,216)">7</span><span>)]</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(187,44,162)">var</span><span> barSet:</span><span style="color:rgb(112,61,170)">Set</span><span>&lt;</span><span style="color:rgb(79,129,135)">Bar</span><span>&gt; = [</span><span style="color:rgb(79,129,135)">Bar</span><span>(</span><span style="color:rgb(39,42,216)">8</span><span>), </span><span style="color:rgb(79,129,135)">Bar</span><span>(</span><span style="color:rgb(39,42,216)">7</span><span>), </span><span style="color:rgb(79,129,135)">Bar</span><span>(</span><span style="color:rgb(39,42,216)">6</span><span>), </span><span style="color:rgb(79,129,135)">Bar</span><span>(</span><span style="color:rgb(39,42,216)">5</span><span>)]</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span></span><br></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span>//fooSet.subtract(barSet) // error: cannot invoke &#39;subtract&#39; with an argument list of type &#39;(Set&lt;Bar&gt;)&#39;</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(79,129,135)"><span>fooSet</span><span> = </span><span>fooSet</span><span>.</span><span style="color:rgb(61,29,129)">subtract</span><span>(</span><span>barSet</span><span> </span><span style="color:rgb(187,44,162)">as</span><span> </span><span style="color:rgb(112,61,170)">Set</span><span>&lt;</span><span>Foo</span><span>&gt;) </span><span style="color:rgb(0,132,0);font-family:menlo">// works, but not what we want</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="color:rgb(79,129,135)">fooSet</span><span>.</span><span style="color:rgb(61,29,129)">forEach</span><span> { </span><span style="color:rgb(61,29,129)">print</span><span>(</span><span style="color:rgb(209,47,27)">&quot;</span><span>\</span><span style="color:rgb(209,47,27)">(</span><span>$0.</span><span style="color:rgb(187,44,162)">dynamicType</span><span style="color:rgb(209,47,27)">), id:</span><span>\</span><span style="color:rgb(209,47,27)">(</span><span>$0.</span><span style="color:rgb(79,129,135)">id</span><span style="color:rgb(209,47,27)">)&quot;</span><span>) }</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span>/*</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span> Foo, id:7</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span> Foo, id:10</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span> Foo, id:9</span></div><div style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span>*/</span></div></div></div></div></blockquote><br></div></div></div><div>This isn&#39;t really a sensible thing to do. The rules for Hashable require that `a == b` implies `a.hashValue == b.hashValue`, and `a.hashValue != b.hashValue` implies `a != b`. If you break these rules you&#39;re going to have problems no matter what static types you&#39;re using.</div><div><br></div><div>Upcasting from Set&lt;Bar&gt; to Set&lt;Foo&gt; is the most concise way to solve this problem.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Jordan</div><br></font></span></div></blockquote></div><br></div>