<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 "Can we `override var hashValue`", I was and am on the side of definitely "Yes". And Jordan Rose, was on the side of perhaps, "but someone should carefully implemented the `hashValue` on base class and subclasses, so that the rule "if `a==b`, then `a.hashValue==b.hashValue` wouldn't be violated". </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's. I knew I was gotten the point of " the base of equality should be the types of instances equaled at first" from somewhere. So I looked up. Turned out I found it in the book "Core Java".</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 "since the hashValue on the base class was not violated the rule, the rule was not broken" was wrong. However, Jordan's point "someone should carefully implemented the `hashValue` on base class and subclass, so that the rule "if `a==b`, then `a.hashValue==b.hashValue` wouldn't be violated. " 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 " Can we `override var hashValue`":</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'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'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"><<a href="mailto:lzell11@gmail.com" target="_blank">lzell11@gmail.com</a>></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't. What'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 "common fruit", 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("Computing hash value!") <br>
return 1 <br>
<span> </span>} <br>
} <br>
var f1 = Foo()<br>
var f2 = Foo()<br>
f1 == f2 // Doesn't print anything!<br>
var aSet = Set<Foo>()<br>
aSet.insert(f1) // Prints "Computing has value!"</p></div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Lou</div></font></span></div>
</blockquote></div><br></div>