<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 5, 2017, at 7:45 AM, Karl Wagner <<a href="mailto:razielim@gmail.com" class="">razielim@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">I have a question about current dispatching behaviour with protocols and ‘Self’.</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><blockquote class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><font face="Courier" class="">protocol CustomEquatable {</font></div><div class=""><font face="Courier" class=""> func equal(to: Self) -> Bool</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">open class Super : CustomEquatable {</font></div><div class=""><font face="Courier" class=""> func equal(to: Super) -> Bool { print("super"); return false }</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><font face="Courier" class="">class Sub: Super {</font></div><div class=""><font face="Courier" class=""> func equal(to: Sub) -> Bool { print("sub-sub"); return true }</font></div><div class=""><font face="Courier" class=""> override func equal(to: Super) -> Bool { print("sub-super"); return true }</font></div><div class=""><font face="Courier" class="">}</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">Sub().equal(to: Sub()) // sub-sub</font></div><div class=""><font face="Courier" class="">Sub().equal(to: Super()) // sub-super </font></div><div class=""><font face="Courier" class="">Super().equal(to: Sub()) // super</font></div><div class=""><font face="Courier" class="">Super().equal(to: Super()) // super</font></div><div class=""><font face="Courier" class=""><br class=""></font></div><div class=""><font face="Courier" class="">(Sub() as Super).equal(to: Sub) // sub-super — dynamically dispatched to callee’s type, not param</font></div><div class=""><font face="Courier" class="">(Sub() as Super).equal(to: (Sub() as Super)) // sub-super — as above</font></div></blockquote><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Currently, we dynamically dispatch to the callee’s type to find ‘Self’, but we don’t apply that consistently when dispatching to ‘Self’-type parameters. Is that expected behaviour?</div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><br class=""></div><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;">- Karl</div></div></blockquote></div><br class=""><div class="">Hi Karl,</div><div class=""><br class=""></div><div class="">This is expected behavior. The choice of method overload is based on the static types: `Super.equal(to: Super)`. The dynamic dispatch that you get when using the override keyword is based on the `self` argument’s dynamic type only. The dynamic types of the non-self arguments don’t play a part.</div><div class=""><br class=""></div><div class="">I’m sure there are some good language design reasons not to support dynamic method overloads. I can only tell you that implementing it would be no fun.</div><div class=""><br class=""></div><div class="">-Andy</div></body></html>