<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=""><div class="">Looks like a bug according to dispatch rules (<a href="https://medium.com/ios-os-x-development/swift-protocol-extension-method-dispatch-6a6bf270ba94#.8zlhlu8s6" class="">here</a>&nbsp;and&nbsp;<a href="https://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future?utm_campaign=iOS+Dev+Weekly&amp;utm_medium=web&amp;utm_source=iOS_Dev_Weekly_Issue_203" class="">here</a>. Also similar question on&nbsp;<a href="http://stackoverflow.com/questions/34847318/swift-protocol-extension-method-dispatch-with-superclass-and-subclass" class="">SO</a>)</div><div class="">It should call dynamic <i class="">bar </i>implementation</div><br class=""><div><blockquote type="cite" class=""><div class="">20 сент. 2016 г., в 5:11, Zhao Xin via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; написал(а):</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_default" style="font-family:georgia,serif">See below code.</div><div class="gmail_default" style="font-family:georgia,serif"><br class=""></div><div class="gmail_default"><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(4, 51, 255);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">protocol</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> Foo {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> bar()</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(4, 51, 255);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">Foo</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> bar() {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(180, 38, 26);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures:no-common-ligatures" class="">"I am bar."</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; }</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> A:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">Foo</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> output() {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(type(of:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">self</span><span style="font-variant-ligatures:no-common-ligatures" class="">)) </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,143,0)" class="">// prints "B".</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(0, 143, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">bar</span><span style="font-variant-ligatures: no-common-ligatures;" class="">() </span><span style="font-variant-ligatures:no-common-ligatures" class="">// prints "I am bar."</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; (self </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">as</span><span style="font-variant-ligatures:no-common-ligatures" class="">! </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">B</span><span style="font-variant-ligatures:no-common-ligatures" class="">).</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">bar</span><span style="font-variant-ligatures:no-common-ligatures" class="">() </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,143,0)" class="">// prints "I am B."</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; }</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">class</span><span style="font-variant-ligatures:no-common-ligatures" class=""> B:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">A</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> bar() {</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">print</span><span style="font-variant-ligatures:no-common-ligatures" class="">(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(180,38,26)" class="">"I am B."</span><span style="font-variant-ligatures:no-common-ligatures" class="">)</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; }</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal;" class=""><span style="font-variant-ligatures:no-common-ligatures;color:rgb(4,51,255)" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> b = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(52,149,175)" class="">B</span><span style="font-variant-ligatures:no-common-ligatures" class="">()</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(52, 149, 175);" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">b</span><span style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures:no-common-ligatures" class="">output</span><span style="font-variant-ligatures: no-common-ligatures;" class="">()</span></div><div style="font-family: menlo; margin: 0px; font-size: 11px; line-height: normal; color: rgb(52, 149, 175);" class=""><br class=""></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">I thought `self.bar()` would do the same as `(self as! B).bar()`. It didn't. In my opinion, &nbsp;`type(of:self) is B.type`, so they should be the same, shouldn't they?</font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class=""><br class=""></font></div><div style="margin: 0px; line-height: normal;" class=""><font face="georgia, serif" class="">Zhaoxin</font></div></div></div>
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>