<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 1, 2016, at 15:44, Zhao Xin via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">Hi Nick,</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default" style="font-family:georgia,serif">Glad to help.</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></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 class=""></blockquote><div class=""><br class=""></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 class=""></div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default"><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">import</span><span style="font-variant-ligatures:no-common-ligatures" class=""> Foundation</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> ==(lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Bool</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">id</span><span style="font-variant-ligatures:no-common-ligatures" class=""> == rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">id</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">class</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> Foo:</span><span style="font-variant-ligatures:no-common-ligatures" class="">Hashable</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> id:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> hashValue: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">id</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><br class="webkit-block-placeholder"></p><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">required</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">init</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">_</span><span style="font-variant-ligatures:no-common-ligatures" class=""> id:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class="">) {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">self</span><span style="font-variant-ligatures:no-common-ligatures" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">id</span><span style="font-variant-ligatures:no-common-ligatures" class=""> = id</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> Bar:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">override</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> hashValue: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Int</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">id</span><span style="font-variant-ligatures:no-common-ligatures" class=""> * </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">5</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> }</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> fooSet:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Set</span><span style="font-variant-ligatures:no-common-ligatures" class=""><</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="">> = [</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">10</span><span style="font-variant-ligatures:no-common-ligatures" class="">), </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">9</span><span style="font-variant-ligatures:no-common-ligatures" class="">), </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">8</span><span style="font-variant-ligatures:no-common-ligatures" class="">), </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">7</span><span style="font-variant-ligatures:no-common-ligatures" class="">)]</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">var</span><span style="font-variant-ligatures:no-common-ligatures" class=""> barSet:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Set</span><span style="font-variant-ligatures:no-common-ligatures" class=""><</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Bar</span><span style="font-variant-ligatures:no-common-ligatures" class="">> = [</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Bar</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">8</span><span style="font-variant-ligatures:no-common-ligatures" class="">), </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Bar</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">7</span><span style="font-variant-ligatures:no-common-ligatures" class="">), </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Bar</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">6</span><span style="font-variant-ligatures:no-common-ligatures" class="">), </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Bar</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">5</span><span style="font-variant-ligatures:no-common-ligatures" class="">)]</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">//fooSet.subtract(barSet) // error: cannot invoke 'subtract' with an argument list of type '(Set<Bar>)'</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(79, 129, 135);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">fooSet</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> = </span><span style="font-variant-ligatures:no-common-ligatures" class="">fooSet</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="">subtract</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures:no-common-ligatures" class="">barSet</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">as</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">Set</span><span style="font-variant-ligatures: no-common-ligatures;" class=""><</span><span style="font-variant-ligatures:no-common-ligatures" class="">Foo</span><span style="font-variant-ligatures: no-common-ligatures;" class="">>) </span><span style="color:rgb(0,132,0);font-family:menlo" class="">// works, but not what we want</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">fooSet</span><span style="font-variant-ligatures:no-common-ligatures" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="">forEach</span><span style="font-variant-ligatures:no-common-ligatures" class=""> { </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="">"</span><span style="font-variant-ligatures:no-common-ligatures" class="">\</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="">(</span><span style="font-variant-ligatures:no-common-ligatures" class="">$0.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)" class="">dynamicType</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="">), id:</span><span style="font-variant-ligatures:no-common-ligatures" class="">\</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="">(</span><span style="font-variant-ligatures:no-common-ligatures" class="">$0.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">id</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)" class="">)"</span><span style="font-variant-ligatures:no-common-ligatures" class="">) }</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">/*</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> Foo, id:7</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> Foo, id:10</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""> Foo, id:9</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">*/</span></div></div></div></div></blockquote><br class=""></div><div>This isn'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're going to have problems no matter what static types you're using.</div><div><br class=""></div><div>Upcasting from Set<Bar> to Set<Foo> is the most concise way to solve this problem.</div><div><br class=""></div><div>Jordan</div><br class=""></body></html>