<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="">On Dec 8, 2015, at 10:32 PM, Frederick Kellison-Linn via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Currently, implementing an ā€˜<font face="SourceCodePro-Regular" class="">==</font>ā€˜ function for a custom class can be somewhat cumbersome. In the simple hierarchy:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(170, 35, 145);" class="">class<span style="" class=""> A {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">var</span> a: <span style="font-variant-ligatures: no-common-ligatures; color: #5c2699" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">_</span> a: <span style="font-variant-ligatures: no-common-ligatures; color: #5c2699" class="">Int</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">a</span> = a</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">}</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro'; min-height: 14px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">class</span> B : <span style="font-variant-ligatures: no-common-ligatures; color: #406e74" class="">A</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">var</span> b: <span style="font-variant-ligatures: no-common-ligatures; color: #5c2699" class="">Int</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">init</span>(<span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">_</span> b: <span style="font-variant-ligatures: no-common-ligatures; color: #5c2699" class="">Int</span>, <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">_</span> a: <span style="font-variant-ligatures: no-common-ligatures; color: #5c2699" class="">Int</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">b</span> = b</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">super</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">init</span>(a)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class="">}</div></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class="">A reasonable implementation of <font face="SourceCodePro-Regular" class="">==</font>&nbsp;would seem to be</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: 'Source Code Pro';" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="font-size: 11px; font-family: 'Source Code Pro'; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">func</span> ==(lhs: <span style="font-variant-ligatures: no-common-ligatures; color: #406e74" class="">A</span>, rhs: <span style="font-variant-ligatures: no-common-ligatures; color: #406e74" class="">A</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #5c2699" class="">Bool</span> {</div><div style="font-size: 11px; font-family: 'Source Code Pro'; margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">return</span> lhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">a</span> == rhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">a</span></div><div style="font-size: 11px; font-family: 'Source Code Pro'; margin: 0px; line-height: normal;" class="">}</div><div style="font-size: 11px; font-family: 'Source Code Pro'; margin: 0px; line-height: normal; min-height: 14px;" class=""><br class=""></div><div style="font-size: 11px; font-family: 'Source Code Pro'; margin: 0px; line-height: normal;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">func</span> ==(lhs: <span style="font-variant-ligatures: no-common-ligatures; color: #406e74" class="">B</span>, rhs: <span style="font-variant-ligatures: no-common-ligatures; color: #406e74" class="">B</span>) -&gt; <span style="font-variant-ligatures: no-common-ligatures; color: #5c2699" class="">Bool</span> {</div><div style="font-size: 11px; font-family: 'Source Code Pro'; margin: 0px; line-height: normal;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #aa2391" class="">return</span> lhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">a</span> == rhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">a</span> &amp;&amp;</div><div style="font-size: 11px; font-family: 'Source Code Pro'; margin: 0px; line-height: normal;" class="">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">b</span> == rhs.<span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">b</span></div><div style="font-size: 11px; font-family: 'Source Code Pro'; margin: 0px; line-height: normal;" class="">}</div><div style="font-size: 11px; font-family: 'Source Code Pro'; margin: 0px; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class=""><div style="margin: 0px; line-height: normal;" class="">Iā€™d be interested to know if there is any interest taken in this problem and whether possible solutions have been discussed. If <font face="SourceCodePro-Regular" class="">==</font>&nbsp;were instead behaved as if it were a method of its first argument (as in a <font face="SourceCodePro-Regular" class="">.equals(other)</font>&nbsp;method) then the solution above is sufficient to avoid returning the wrong result, but being forced to use&nbsp;<span style="font-family: 'Source Code Pro';" class="">.</span><span style="font-family: 'Source Code Pro'; color: rgb(170, 35, 145);" class="">dynamicType</span><span style="font-family: 'Source Code Pro';" class="">&nbsp;</span>for something as basic as equality checking seems cumbersome to me.</div></div></div></div></div></div></blockquote><br class=""></div><div>Hi Frederick,</div><div><br class=""></div><div>The preferred approach is to allow operators to be defined inside of types. &nbsp;This would allow them to be dynamically dispatched inside of classes.</div><div><br class=""></div><div>-Chris</div><br class=""></body></html>