<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=""><div class="">As I’ve said before, this doesn’t work.</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class="">let x: Fruit = Apple(“a”, shape: .medium)</div><div class="">let y: Fruit = Apple(“a”, shape: .big)</div><div class="">print(x == y) // will print true</div></blockquote><div class=""><br class=""></div><div class="">If you want == to behave differently for subclasses, it needs to call a method that can be overridden. Static overloads will not produce the same behavior.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 2, 2016, at 18:42, 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">The key is how to write the `==` function. It should compare the` dynamicType`(or `type(of:)` in Swift 3.0) if the class is not a final 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="">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="">Fruit</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="">Fruit</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><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(61,29,129)" class="">print</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="">hashValue</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(61,29,129)" class="">print</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="">hashValue</span><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="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> type(of:lhs) == type(of:rhs) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="">&&</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="">name</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="">==</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="">name</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="">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="">Apple</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="">Apple</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=""> type(of:lhs) == type(of:rhs) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="">&&</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="">name</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="">==</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="">name</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="">&&</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="">shape</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="">shape</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="">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="">Banana</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="">Banana</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=""> type(of:lhs) == type(of:rhs) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)" class="">&&</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="">name</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="">==</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="">name</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="">&&</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="">shape</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="">shape</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; 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=""> Fruit:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" 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=""> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">String</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="">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(39,42,216)" class="">0</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="">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=""> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">String</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="">"common fruit"</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="">name</span><span style="font-variant-ligatures:no-common-ligatures" class=""> = name</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="">enum</span><span style="font-variant-ligatures:no-common-ligatures" class=""> FruitShape:</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="">case</span><span style="font-variant-ligatures:no-common-ligatures" class=""> small = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">1000</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="">case</span><span style="font-variant-ligatures:no-common-ligatures" class=""> medium = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">2000</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="">case</span><span style="font-variant-ligatures:no-common-ligatures" class=""> big = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)" class="">3000</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=""> Apple:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Fruit</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=""> shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">FruitShape</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="">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(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><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=""> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">String</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="">"common fruit"</span><span style="font-variant-ligatures:no-common-ligatures" class="">, shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">FruitShape</span><span style="font-variant-ligatures:no-common-ligatures" class=""> = .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)" class="">medium</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="">shape</span><span style="font-variant-ligatures:no-common-ligatures" class=""> = shape</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="">super</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="">(name)</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=""> Banana:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Fruit</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=""> shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">FruitShape</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="">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(39,42,216)" class="">10</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=""> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)" class="">String</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="">"common fruit"</span><span style="font-variant-ligatures:no-common-ligatures" class="">, shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">FruitShape</span><span style="font-variant-ligatures:no-common-ligatures" class=""> = .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)" class="">medium</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="">shape</span><span style="font-variant-ligatures:no-common-ligatures" class=""> = shape</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="">super</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="">(name)</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="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> apple = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Apple</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="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> banana = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)" class="">Banana</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(79, 129, 135);" class=""><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" class="">apple</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)" class="">==</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures" class="">banana</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=""> 5</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=""> 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=""> false</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=""><br class=""></span></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">I got the idea from book "Core Java", mine is version 8, the latest is the version 10. I learnt how to writing Object oriented code from it. I am glad it is still useful.</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">Zhaoxin</font></div></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, Sep 3, 2016 at 9:14 AM, Zhao Xin <span dir="ltr" class=""><<a href="mailto:owenzx@gmail.com" target="_blank" class="">owenzx@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif"><span class=""><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"><span style="font-family:arial,sans-serif;font-size:13px" class="">There is no reason to compare the shape, it is a constant in each of</span></blockquote><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"><span style="font-family:arial,sans-serif;font-size:13px" class="">these types. (So I am not sure what your point is.)</span></blockquote><div class=""><br class=""></div></span><div class="">Sorry. The `let shape` should be `var shape`. I just wanted to make the subclass to be something more than the super class.</div><span class=""><div class=""><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"><span style="font-family:arial,sans-serif;font-size:13px" class="">If two values are equal, their hash values should be equal. As long<br class=""></span><span style="font-family:arial,sans-serif;font-size:13px" class="">as your override implementation guarantees this, you can override<br class=""></span><span style="font-family:arial,sans-serif;font-size:13px" class="">hashValue.</span></blockquote><div class=""><br class=""></div></span><div class="">But the question is how? If this must be guaranteed by the subclass, how to writing the override? Or it just can't be done?</div><span class=""><div class=""><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"><span style="font-family:arial,sans-serif;font-size:13px" class="">You should also understand that the ==(Apple, Apple) and ==(Banana,<br class=""></span><span style="font-family:arial,sans-serif;font-size:13px" class="">Banana) are not overrides for ==(Fruit, Fruit), and they would not be<br class=""></span><span style="font-family:arial,sans-serif;font-size:13px" class="">called through dynamic dispatch when you have, for example, two apples<br class=""></span><span style="font-family:arial,sans-serif;font-size:13px" class="">typed as fruits.</span></blockquote><div class=""><br class=""></div></span><div class="">In fact, in my example code, `apple` and `banana` is instance of `Apple` and `Banana`. They are not using `let apple:Fruit = Apple()`. The `==` used the <b class="">`Fruit` version</b> as it was the only appropriate one. My big question is, since they used the `<b class="">Fruit` version</b>, and the <b class="">`Fruit` version of `hashValue`</b> could guarantee the `hashValue` equality, isn't that enough?</div><div class=""><br class=""></div><div class="">Zhaoxin</div><div class=""> </div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br class=""><div class="gmail_quote">On Sat, Sep 3, 2016 at 7:02 AM, Dmitri Gribenko <span dir="ltr" class=""><<a href="mailto:gribozavr@gmail.com" target="_blank" class="">gribozavr@gmail.com</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On Sat, Sep 3, 2016 at 1:31 AM, Zhao Xin via swift-users<br class="">
<<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>> wrote:<br class="">
> func ==(lhs: Apple, rhs: Apple) -> Bool {<br class="">
> return <a href="http://lhs.name/" rel="noreferrer" target="_blank" class="">lhs.name</a> == <a href="http://rhs.name/" rel="noreferrer" target="_blank" class="">rhs.name</a> && lhs.shape == rhs.shape<br class="">
> }<br class="">
><br class="">
> func ==(lhs: Banana, rhs: Banana) -> Bool {<br class="">
> return <a href="http://lhs.name/" rel="noreferrer" target="_blank" class="">lhs.name</a> == <a href="http://rhs.name/" rel="noreferrer" target="_blank" class="">rhs.name</a> && lhs.shape == rhs.shape<br class="">
> }<br class="">
<br class="">
</span>There is no reason to compare the shape, it is a constant in each of<br class="">
these types. (So I am not sure what your point is.)<br class="">
<span class=""><br class="">
> My question is, apple equals banana, but their hashValues (in their own<br class="">
> types) don't. What's wrong here? Is that means we shouldn't override<br class="">
> hashValue in subclass in Swift?<br class="">
<br class="">
</span>This means you should not override hashValue in this particular way.<br class="">
If two values are equal, their hash values should be equal. As long<br class="">
as your override implementation guarantees this, you can override<br class="">
hashValue.<br class="">
<br class="">
You should also understand that the ==(Apple, Apple) and ==(Banana,<br class="">
Banana) are not overrides for ==(Fruit, Fruit), and they would not be<br class="">
called through dynamic dispatch when you have, for example, two apples<br class="">
typed as fruits.<br class="">
<span class=""><font color="#888888" class=""><br class="">
Dmitri<br class="">
<br class="">
--<br class="">
main(i,j){for(i=2;;i++){for(j=<wbr class="">2;j<i;j++){if(!(i%j)){j=0;brea<wbr class="">k;}}if<br class="">
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank" class="">gribozavr@gmail.com</a>>*/<br class="">
</font></span></blockquote></div><br class=""></div>
</div></div></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>