<div dir="ltr"><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></div><div class="gmail_default"><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> ==(lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Fruit</span><span style="font-variant-ligatures:no-common-ligatures">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Fruit</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">print</span><span style="font-variant-ligatures:no-common-ligatures">(lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">hashValue</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">print</span><span style="font-variant-ligatures:no-common-ligatures">(rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">hashValue</span><span style="font-variant-ligatures:no-common-ligatures">)</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> type(of:lhs) == type(of:rhs) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">==</span><span style="font-variant-ligatures:no-common-ligatures"> rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> ==(lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Apple</span><span style="font-variant-ligatures:no-common-ligatures">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Apple</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> type(of:lhs) == type(of:rhs) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">==</span><span style="font-variant-ligatures:no-common-ligatures"> rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span><span style="font-variant-ligatures:no-common-ligatures"> == rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> ==(lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Banana</span><span style="font-variant-ligatures:no-common-ligatures">, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Banana</span><span style="font-variant-ligatures:no-common-ligatures">) -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Bool</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> type(of:lhs) == type(of:rhs) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">==</span><span style="font-variant-ligatures:no-common-ligatures"> rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">&amp;&amp;</span><span style="font-variant-ligatures:no-common-ligatures"> lhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span><span style="font-variant-ligatures:no-common-ligatures"> == rhs.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">class</span><span style="font-variant-ligatures:no-common-ligatures"> Fruit:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Hashable</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">String</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> hashValue: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">0</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">init</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">String</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">&quot;common fruit&quot;</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">self</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">name</span><span style="font-variant-ligatures:no-common-ligatures"> = name</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">enum</span><span style="font-variant-ligatures:no-common-ligatures"> FruitShape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">case</span><span style="font-variant-ligatures:no-common-ligatures"> small = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">1000</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">case</span><span style="font-variant-ligatures:no-common-ligatures"> medium = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">2000</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">case</span><span style="font-variant-ligatures:no-common-ligatures"> big = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">3000</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">class</span><span style="font-variant-ligatures:no-common-ligatures"> Apple:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Fruit</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">FruitShape</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">override</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> hashValue: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">5</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">required</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">init</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">String</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">&quot;common fruit&quot;</span><span style="font-variant-ligatures:no-common-ligatures">, shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">FruitShape</span><span style="font-variant-ligatures:no-common-ligatures"> = .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">medium</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">self</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span><span style="font-variant-ligatures:no-common-ligatures"> = shape</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">super</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">init</span><span style="font-variant-ligatures:no-common-ligatures">(name)</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">class</span><span style="font-variant-ligatures:no-common-ligatures"> Banana:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Fruit</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">FruitShape</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">override</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">var</span><span style="font-variant-ligatures:no-common-ligatures"> hashValue: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">Int</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(39,42,216)">10</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">required</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">init</span><span style="font-variant-ligatures:no-common-ligatures">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> name:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(112,61,170)">String</span><span style="font-variant-ligatures:no-common-ligatures"> = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(209,47,27)">&quot;common fruit&quot;</span><span style="font-variant-ligatures:no-common-ligatures">, shape:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">FruitShape</span><span style="font-variant-ligatures:no-common-ligatures"> = .</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">medium</span><span style="font-variant-ligatures:no-common-ligatures">) {</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">self</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">shape</span><span style="font-variant-ligatures:no-common-ligatures"> = shape</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">        </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">super</span><span style="font-variant-ligatures:no-common-ligatures">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">init</span><span style="font-variant-ligatures:no-common-ligatures">(name)</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">    }</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">}</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> apple = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Apple</span><span style="font-variant-ligatures:no-common-ligatures">()</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(187,44,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> banana = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Banana</span><span style="font-variant-ligatures:no-common-ligatures">()</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(79,129,135)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(61,29,129)">print</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(</span><span style="font-variant-ligatures:no-common-ligatures">apple</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(49,89,93)">==</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">banana</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">)</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures">/*</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures"> 5</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures"> 10</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures"> false</span></p>
<p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures">*/</span></p><p style="font-family:menlo;margin:0px;font-size:11px;line-height:normal;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000">I got the idea from book &quot;Core Java&quot;, 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></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000"><br></font></p><p style="margin:0px;line-height:normal"><font face="georgia, serif" color="#000000">Zhaoxin</font></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Sep 3, 2016 at 9:14 AM, Zhao Xin <span dir="ltr">&lt;<a href="mailto:owenzx@gmail.com" target="_blank">owenzx@gmail.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 dir="ltr"><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">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">these types.  (So I am not sure what your point is.)</span></blockquote><div><br></div></span><div>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><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"><span style="font-family:arial,sans-serif;font-size:13px">If two values are equal, their hash values should be equal.  As long<br></span><span style="font-family:arial,sans-serif;font-size:13px">as your override implementation guarantees this, you can override<br></span><span style="font-family:arial,sans-serif;font-size:13px">hashValue.</span></blockquote><div><br></div></span><div>But the question is how? If this must be guaranteed by the subclass, how to writing the override? Or it just can&#39;t be done?</div><span class=""><div><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"><span style="font-family:arial,sans-serif;font-size:13px">You should also understand that the ==(Apple, Apple) and ==(Banana,<br></span><span style="font-family:arial,sans-serif;font-size:13px">Banana) are not overrides for ==(Fruit, Fruit), and they would not be<br></span><span style="font-family:arial,sans-serif;font-size:13px">called through dynamic dispatch when you have, for example, two apples<br></span><span style="font-family:arial,sans-serif;font-size:13px">typed as fruits.</span></blockquote><div><br></div></span><div>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>`Fruit` version</b> as it was the only appropriate one. My big question is, since they used the `<b>Fruit` version</b>, and the <b>`Fruit` version of `hashValue`</b> could guarantee the `hashValue` equality, isn&#39;t that enough?</div><div><br></div><div>Zhaoxin</div><div> </div></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Sep 3, 2016 at 7:02 AM, Dmitri Gribenko <span dir="ltr">&lt;<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Sat, Sep 3, 2016 at 1:31 AM, Zhao Xin via swift-users<br>
&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:<br>
&gt; func ==(lhs: Apple, rhs: Apple) -&gt; Bool {<br>
&gt;     return <a href="http://lhs.name" rel="noreferrer" target="_blank">lhs.name</a> == <a href="http://rhs.name" rel="noreferrer" target="_blank">rhs.name</a> &amp;&amp; lhs.shape == rhs.shape<br>
&gt; }<br>
&gt;<br>
&gt; func ==(lhs: Banana, rhs: Banana) -&gt; Bool {<br>
&gt;     return <a href="http://lhs.name" rel="noreferrer" target="_blank">lhs.name</a> == <a href="http://rhs.name" rel="noreferrer" target="_blank">rhs.name</a> &amp;&amp; lhs.shape == rhs.shape<br>
&gt; }<br>
<br>
</span>There is no reason to compare the shape, it is a constant in each of<br>
these types.  (So I am not sure what your point is.)<br>
<span><br>
&gt; My question is, apple equals banana, but their hashValues (in their own<br>
&gt; types)  don&#39;t. What&#39;s wrong here? Is that means we shouldn&#39;t override<br>
&gt; hashValue in subclass in Swift?<br>
<br>
</span>This means you should not override hashValue in this particular way.<br>
If two values are equal, their hash values should be equal.  As long<br>
as your override implementation guarantees this, you can override<br>
hashValue.<br>
<br>
You should also understand that the ==(Apple, Apple) and ==(Banana,<br>
Banana) are not overrides for ==(Fruit, Fruit), and they would not be<br>
called through dynamic dispatch when you have, for example, two apples<br>
typed as fruits.<br>
<span><font color="#888888"><br>
Dmitri<br>
<br>
--<br>
main(i,j){for(i=2;;i++){for(j=<wbr>2;j&lt;i;j++){if(!(i%j)){j=0;brea<wbr>k;}}if<br>
(j){printf(&quot;%d\n&quot;,i);}}} /*Dmitri Gribenko &lt;<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>&gt;*/<br>
</font></span></blockquote></div><br></div>
</div></div></blockquote></div><br></div>