<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
Thanks for the fast response!
<div class=""><br class="">
</div>
<div class="">Replacing the == definition with</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">override</span>
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
func</span> isEqual(object: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
AnyObject</span>?) -> <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">
Bool</span> {</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
guard</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
let</span> object = object <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
as</span>? <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">
KeyType2</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
else</span> { <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
false</span> }</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">
return</span> <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">
id</span> == object.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">id</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
}</div>
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">fixed this issue!</div>
<div class=""><br class="">
</div>
<div class="">Etan</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
<div>
<blockquote type="cite" class="">
<div class="">On 17 Dec 2015, at 23:48, Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" class="">gribozavr@gmail.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div dir="ltr" class="">
<div class="gmail_extra">
<div class="gmail_quote">On Thu, Dec 17, 2015 at 2:17 PM, Etan Kissling via swift-users
<span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class="">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word" class="">I want to use instances of a custom class as Dictionary key.
<div class="">This requires the class to conform to Hashable.</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">func</span> ==(lhs: <span style="color:#4f8187" class="">
KeyType1</span>, rhs: <span style="color:#4f8187" class="">KeyType1</span>) -> <span style="color:#703daa" class="">
Bool</span> { <span style="color:#bb2ca2" class="">return</span> lhs.<span style="color:#4f8187" class="">id</span> == rhs.<span style="color:#4f8187" class="">id</span> }</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="">
<br class="">
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(112,61,170)" class="">
<span style="color:#bb2ca2" class="">final</span><span style="" class=""> </span>
<span style="color:#bb2ca2" class="">class</span><span style="" class=""> KeyType1:
</span>Hashable<span style="" class="">, </span>CustomStringConvertible<span style="" class=""> {</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">let</span> id: <span style="color:#703daa" class="">
String</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">init</span>(id: <span style="color:#703daa" class="">
String</span>) { <span style="color:#bb2ca2" class="">self</span>.<span style="color:#4f8187" class="">id</span> = id }</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">var</span> hashValue: <span style="color:#703daa" class="">
Int</span> { <span style="color:#bb2ca2" class="">return</span> <span style="color:#4f8187" class="">
id</span>.<span style="color:#703daa" class="">hashValue</span> }</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">var</span> description: <span style="color:#703daa" class="">
String</span> { <span style="color:#bb2ca2" class="">return</span> <span style="color:#4f8187" class="">
id </span>}</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
}</div>
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">Now I can use KeyType1 instances as key in Dictionary.</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">var</span> collection1 = [<span style="color:#4f8187" class="">KeyType1</span>(id:
<span style="color:#d12f1b" class="">"foo"</span>) : <span style="color:#703daa" class="">
NSObject</span>()]</div>
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">Testing works fine:</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">let</span> key = <span style="color:#4f8187" class="">
collection1</span>.<span style="color:#703daa" class="">first</span>!.<span style="color:#272ad8" class="">0</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Key stored in collection:
<span style="" class="">\</span>(<span style="color:#3d1d81" class="">unsafeAddressOf</span><span style="" class="">(key)</span>) --
<span style="" class="">\</span>(<span style="" class="">key</span>)"<span style="" class="">)</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">
<br class="">
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">let</span> keyCopy = <span style="color:#4f8187" class="">
KeyType1</span>(id: key.<span style="color:#4f8187" class="">id</span>)</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Key copy:
<span style="" class="">\</span>(<span style="color:#3d1d81" class="">unsafeAddressOf</span><span style="" class="">(keyCopy)</span>) --
<span style="" class="">\</span>(<span style="" class="">keyCopy</span>)"<span style="" class="">)</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">
<br class="">
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Keys equal:
<span style="" class="">\</span>(<span style="" class="">key </span><span style="color:#31595d" class="">==</span><span style="" class=""> keyCopy</span>)"<span style="" class="">)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Hash values equal:
<span style="" class="">\</span>(<span style="" class="">key.</span><span style="color:#4f8187" class="">hashValue</span><span style="" class=""> == keyCopy.</span><span style="color:#4f8187" class="">hashValue</span>)"<span style="" class="">)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Collection has item for key:
<span style="" class="">\</span>(<span style="color:#4f8187" class="">collection1</span><span style="" class="">[key] !=
</span><span style="color:#bb2ca2" class="">nil</span>)"<span style="" class="">)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>"Collection has item for key copy:
<span style="" class="">\</span>(<span style="color:#4f8187" class="">collection1</span><span style="" class="">[keyCopy] !=
</span><span style="color:#bb2ca2" class="">nil</span>)"<span style="" class="">)</span></div>
</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Key stored in collection: 0x0000608000043d80 -- foo</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Key copy: 0x00006080000440b0 -- foo</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Keys equal: true</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Hash values equal: true</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Collection has item for key: true</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class="">Collection has item for key copy: true</b></div>
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">Next, I repeat the same set up -- but this time KeyType is a descendant of NSObject.</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">func</span> ==(lhs: <span style="color:#4f8187" class="">
KeyType2</span>, rhs: <span style="color:#4f8187" class="">KeyType2</span>) -> <span style="color:#703daa" class="">
Bool</span> { <span style="color:#bb2ca2" class="">return</span> lhs.<span style="color:#4f8187" class="">id</span> == rhs.<span style="color:#4f8187" class="">id</span> }</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="">
<br class="">
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)" class="">
<span style="color:#bb2ca2" class="">final</span><span style="" class=""> </span>
<span style="color:#bb2ca2" class="">class</span><span style="" class=""> KeyType2:
</span><span style="color:#703daa" class="">NSObject</span><span style="" class=""> {
</span>// NSObject conforms to Hashable and CustomStringConvertible.</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">let</span> id: <span style="color:#703daa" class="">
String</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">init</span>(id: <span style="color:#703daa" class="">
String</span>) { <span style="color:#bb2ca2" class="">self</span>.<span style="color:#4f8187" class="">id</span> = id;
<span style="color:#bb2ca2" class="">super</span>.<span style="color:#bb2ca2" class="">init</span>() }</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">override</span> <span style="color:#bb2ca2" class="">
var</span> hashValue: <span style="color:#703daa" class="">Int</span> { <span style="color:#bb2ca2" class="">
return</span> <span style="color:#4f8187" class="">id</span>.<span style="color:#703daa" class="">hashValue</span> }</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">override</span> <span style="color:#bb2ca2" class="">
var</span> description: <span style="color:#703daa" class="">String</span> { <span style="color:#bb2ca2" class="">
return</span> <span style="color:#4f8187" class="">id </span>}</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
}</div>
</div>
<div class=""><br class="">
</div>
<div class="">Again, I create a Dictionary based on this key class.</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">var</span> collection2 = [<span style="color:#4f8187" class="">KeyType2</span>(id:
<span style="color:#d12f1b" class="">"foo"</span>) : <span style="color:#703daa" class="">
NSObject</span>()]</div>
</div>
<div class=""><br class="">
</div>
<div class="">Using the same tests, they fail now.</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">let</span> key = <span style="color:#4f8187" class="">
collection2</span>.<span style="color:#703daa" class="">first</span>!.<span style="color:#272ad8" class="">0</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Key stored in collection:
<span style="" class="">\</span>(<span style="color:#3d1d81" class="">unsafeAddressOf</span><span style="" class="">(key)</span>) --
<span style="" class="">\</span>(<span style="" class="">key</span>)"<span style="" class="">)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="">
<br class="">
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<span style="color:#bb2ca2" class="">let</span> keyCopy = <span style="color:#4f8187" class="">
KeyType2</span>(id: key.<span style="color:#4f8187" class="">id</span>)</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Key copy:
<span style="" class="">\</span>(<span style="color:#3d1d81" class="">unsafeAddressOf</span><span style="" class="">(keyCopy)</span>) --
<span style="" class="">\</span>(<span style="" class="">keyCopy</span>)"<span style="" class="">)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px" class="">
<br class="">
</div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Keys equal:
<span style="" class="">\</span>(<span style="" class="">key </span><span style="color:#31595d" class="">==</span><span style="" class=""> keyCopy</span>)"<span style="" class="">)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Hash values equal:
<span style="" class="">\</span>(<span style="" class="">key.</span><span style="color:#4f8187" class="">hashValue</span><span style="" class=""> == keyCopy.</span><span style="color:#4f8187" class="">hashValue</span>)"<span style="" class="">)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>" Collection has item for key:
<span style="" class="">\</span>(<span style="color:#4f8187" class="">collection2</span><span style="" class="">[key] !=
</span><span style="color:#bb2ca2" class="">nil</span>)"<span style="" class="">)</span></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(209,47,27)" class="">
<span style="" class=""> </span><span style="color:#3d1d81" class="">print</span><span style="" class="">(</span>"Collection has item for key copy:
<span style="" class="">\</span>(<span style="color:#4f8187" class="">collection2</span><span style="" class="">[keyCopy] !=
</span><span style="color:#bb2ca2" class="">nil</span>)"<span style="" class="">)</span></div>
</div>
<div class=""><br class="">
</div>
<div class="">
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Key stored in collection: 0x0000608000044080 -- foo</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Key copy: 0x00006080000440e0 -- foo</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Keys equal: true</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Hash values equal: true</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class=""> Collection has item for key: true</b></div>
<div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class="">
<b class="">Collection has item for key copy: false</b></div>
</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
</div>
<div class="">What am I missing here?</div>
</div>
</blockquote>
<div class=""><br class="">
</div>
<div class="">The == overload in the second case is not the one that gets put into the protocol witness table. When you call == on two instances of your type, upcast to NSObject, the isEqual() method is called.</div>
<div class=""><br class="">
</div>
<div class="">We are aware of this issue and it will be fixed when we move operators into types.</div>
<div class=""><br class="">
</div>
<div class="">Dmitri</div>
</div>
<div class=""><br class="">
</div>
-- <br class="">
<div class="gmail_signature">main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br class="">
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank" class="">gribozavr@gmail.com</a>>*/</div>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>