<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">Thanks Lou. </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"><span style="font-family:arial,sans-serif;font-size:13px">In addition to what Jordan and Dmitri have said, I think part of the confusion is that you are assuming hash values are implicitly used in an equality check.  They are not.  They are used when your instances are added to certain types of collections.</span></blockquote><div><br></div><div class="gmail_default" style="font-family:georgia,serif">​You are very nice. But I was not assuming hash values were implicitly used in an equality check. In fact, my problem was solved on the third email of mine in this thread.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Here is the history of the question and the answer.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">1. On the point of &quot;Can we `override var hashValue`&quot;, I was and am on the side of definitely &quot;Yes&quot;. And Jordan Rose, was on the side of perhaps, &quot;but someone should carefully implemented the `hashValue` on base class and subclasses, so that the rule &quot;if `a==b`, then `a.hashValue==b.hashValue` wouldn&#39;t be violated&quot;. </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">2. At the beginning, I still thought we could `override var hashValue`. But it violated the rule with comparing between instances from different subclasses. My explanation was that since the hashValue on the base class was not violated the rule, the rule was not broken. Also I insisted that thought it was not documented, the base of equality should be the types of instances equaled at first.  I knew my point was weak, so I asked in this thread to see if there was something I missed.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">3. I got the reply from Michael Nisi. But I thought his point was alike Jordan&#39;s. I knew I was gotten the point of &quot; the base of equality should be the types of instances equaled at first&quot; from somewhere. So I looked up. Turned out I found it in the book &quot;Core Java&quot;.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">4. The conclusion: The rule should not be violated at any time. My previous explanation of &quot;since the hashValue on the base class was not violated the rule, the rule was not broken&quot; was wrong. However, Jordan&#39;s point &quot;someone should carefully implemented the `hashValue` on base class and subclass, so that the rule &quot;if `a==b`, then `a.hashValue==b.hashValue` wouldn&#39;t be violated. &quot; was also not correct. As there was no way to `override hashValue` like that.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Here are my points on &quot; Can we `override var hashValue`&quot;:</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">1. We can. But we should firstly test the `type(of:lhs) == type(of:rhs)`. If that doesn&#39;t equal, we return false. This technique solved the problem of mine in this thread. This should be the first choice of all situations.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">2. We can but we may prefer not to. If we choose not to, we must keep that to all subclasses of the base class. I don&#39;t prefer this way, but someone may like it. I think doing this may gain more problems than it solves. But again, since it does not violate any documented rules, it could exist.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">3. In both 1 and 2, we must also write `==` for each subclasses.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Sep 7, 2016 at 5:55 AM, Lou Zell <span dir="ltr">&lt;<a href="mailto:lzell11@gmail.com" target="_blank">lzell11@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"><span class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="color:rgb(0,0,0);font-family:georgia,serif;font-size:12.8px">My question is, apple equals banana, but their hashValues (in their own types)  don&#39;t. What&#39;s wrong here? </span></blockquote>​</span><div>Hi Zhao.  In addition to what Jordan and Dmitri have said, I think part of the confusion is that you are assuming hash values are implicitly used in an equality check.  They are not.  They are used when your instances are added to certain types of collections. </div><div><br></div><div>In your first example, where you print out the hash values but then compare <a href="http://lhs.name" target="_blank">lhs.name</a> to <a href="http://rhs.name" target="_blank">rhs.name</a>, the names of the two fruits are both &quot;common fruit&quot;, and the equality test returns true.  Hash never comes into play.  You can test for yourself when the hash gets used:</div><div><br></div><div>







<p>import Foundation<br>
class Foo: NSObject { <br>
<span>    </span>override var hash: Int { <br>
<span>        </span>print(&quot;Computing hash value!&quot;) <br>
        return 1 <br>
<span>    </span>} <br>
} <br>
var f1 = Foo()<br>
var f2 = Foo()<br>
f1 == f2  // Doesn&#39;t print anything!<br>
var aSet = Set&lt;Foo&gt;()<br>
aSet.insert(f1)  // Prints &quot;Computing has value!&quot;</p></div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Lou</div></font></span></div>
</blockquote></div><br></div>