Yep, my example contains an empty extension C:P - as you correctly notes, that way it compiles. <br><br>I did make a mistake when writing the original email, but corrected it a couple of seconds later, so I stand by the idea that this logic is deduceable :)<br><br><div>In both cases the last line is calling something that can be denoted as <span></span> (C() as P).P_f <br>Now, if we write the effects of the line marked &quot;remember this line&quot; in pseudocode as <br><br>class C:P {<br>  override P_f = C_f // effect of C:P if &quot;remember&quot; is present <br>} <br><br>the difference between cases is rather logical. <br><div class="gmail_quote"><div dir="ltr">On Tue, Dec 8, 2015 at 01:42 Paul Cantrell &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;cantrell@pobox.com&#39;);" target="_blank">cantrell@pobox.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>I must admit I&#39;m having difficulty understanding why it&#39;s a big deal whether the dispatch will be static or dynamic. This seems like an implementation detail; any &quot;dynamic dispatch&quot; in the aforementioned sense can actually became static for a final class.</div></div></div></div></blockquote></div></div><div style="word-wrap:break-word"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>It’s not just an implementation detail. The original article demonstrates this compellingly, I think.</div><div><br></div><div>Yes, it’s true that, as an optimization, the compiler can choose to use static dispatch in situations where doing so makes no difference (e.g. calling a final method, calling a private method with no overrides, etc.).</div><div><br></div><div>However, there are situations where static dispatch changes the behavior of the code. At that point, it’s a semantic difference. It’s those cases I’m concerned about.</div></div></div></div></div></div><div style="word-wrap:break-word"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div><blockquote type="cite"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Just think about function defined as having a &quot;magic prefix&quot; that corresponds technically to vtable where they can be located:</div></div></div></div></blockquote></div></div></div></div></div></div><div style="word-wrap:break-word"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"></div></div></div></div><div><br></div><div>It’s not that it’s hard to understand what’s happening if you <i>already know</i> that a call uses static dispatch. The problem is that it’s difficult to determine <i>whether it does</i>.</div><div><br></div><div>• • •</div><div><br></div><div>Note that your example code with C_f and P_f does not demonstrate the problem at hand. It’s worth working through why.</div><div><br></div><div>Translating your pseudocode into actual Swift, this does not compile:</div><div><br></div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo"><div style="margin:0px;font-size:10.5px;line-height:normal">    <span style="color:#323e7d">class</span> C {</div><div style="margin:0px;font-size:10.5px;line-height:normal">        <span style="color:#323e7d">func</span> f() {</div><div style="margin:0px;font-size:10.5px;line-height:normal">            <span style="color:#587ea8">print</span>(<span style="color:#843e64">&quot;C_f&quot;</span>)</div><div style="margin:0px;font-size:10.5px;line-height:normal">        }</div><div style="margin:0px;font-size:10.5px;line-height:normal">    }</div><div style="margin:0px;font-size:10.5px;line-height:normal;min-height:12px"><br></div><div style="margin:0px;font-size:10.5px;line-height:normal">    <span style="color:#323e7d">protocol</span> P {</div><div style="margin:0px;font-size:10.5px;line-height:normal">        <span style="color:#323e7d">func</span> f()</div><div style="margin:0px;font-size:10.5px;line-height:normal">    }</div><div style="margin:0px;font-size:10.5px;line-height:normal;min-height:12px"><br></div><div style="margin:0px;font-size:10.5px;line-height:normal">    <span style="color:#323e7d">extension</span> <span style="color:#587ea8">C</span>: <span style="color:#587ea8">P</span> {</div><div style="margin:0px;font-size:10.5px;line-height:normal;color:rgb(102,139,73)"><span style="color:#000000">        </span><span style="color:#323e7d">func</span><span style="color:#000000"> f() {  </span>// compiler error here</div><div style="margin:0px;font-size:10.5px;line-height:normal">            <span style="color:#587ea8">print</span>(<span style="color:#843e64">&quot;P_f&quot;</span>)</div><div style="margin:0px;font-size:10.5px;line-height:normal">        }</div><div style="margin:0px;font-size:10.5px;line-height:normal">    }</div><div><br></div></div><div>I imagine that you were thinking of something along these lines:</div><div><br></div><div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">    <span style="color:#323e7d">class</span> C {</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">        <span style="color:#323e7d">func</span> f() {</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">            <span style="color:#587ea8">print</span>(<span style="color:#843e64">&quot;C_f&quot;</span>)</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">        }</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo;min-height:12px"><br></div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">    <span style="color:#323e7d">protocol</span> P {</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">        <span style="color:#323e7d">func</span> f()  <span style="color:rgb(102,139,73)">// remember this line</span></div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo;min-height:12px"><br></div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo;color:rgb(50,62,125)"><span style="color:#000000">    </span>extension<span style="color:#000000"> </span><span style="color:#587ea8">P</span><span style="color:#000000"> {</span></div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo;color:rgb(102,139,73)"><span style="color:#000000">        </span><span style="color:#323e7d">func</span><span style="color:#000000"> f() {</span></div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">            <span style="color:#587ea8">print</span>(<span style="color:#843e64">&quot;P_f&quot;</span>)</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">        }</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">    }</div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo;min-height:12px"><br></div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">    <span style="color:#323e7d">extension</span> <span style="color:#587ea8">C</span>: <span style="color:#587ea8">P</span> { }  <span style="color:rgb(102,139,73)">// C: P now separate from extension impl of f()</span></div><p style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo;min-height:12px"><span style="font-size:10.5px"><br></span></p><div>However, this does not behave as you think it does:</div><div><br></div><p style="margin:0px;line-height:normal;min-height:12px"><span style="font-family:Menlo;font-size:10.5px">    </span><span style="font-family:Menlo;font-size:10.5px">(</span><span style="font-family:Menlo;font-size:10.5px;color:rgb(88,126,168)">C</span><span style="font-family:Menlo;font-size:10.5px">() </span><span style="font-family:Menlo;font-size:10.5px;color:rgb(50,62,125)">as</span><span style="font-family:Menlo;font-size:10.5px"> </span><span style="font-family:Menlo;font-size:10.5px;color:rgb(88,126,168)">C</span><span style="font-family:Menlo;font-size:10.5px">).</span><span style="font-family:Menlo;font-size:10.5px;color:rgb(88,126,168)">f</span><span style="font-family:Menlo;font-size:10.5px">()</span><font face="Menlo" size="1">  </font><font color="#668b49" face="Menlo" size="1">// </font><font color="#668b49" face="Menlo"><font size="1">C_f</font></font></p><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">    (<span style="color:#587ea8">C</span>() <span style="color:#323e7d">as</span> <span style="color:#587ea8">P</span>).<span style="color:#587ea8">f</span>()<font face="Menlo" size="1">  </font><font color="#668b49" face="Menlo" size="1">// </font><font color="#668b49" face="Menlo"><font size="1">C_f</font></font></div></div><div><br></div><div>However <i>again</i>, if you remove the line marked “remember this line,” <i>then</i> the code does do what you think it does:</div><div><br></div><div><div><div style="margin:0px;line-height:normal;min-height:12px"><span style="font-family:Menlo;font-size:10.5px">    </span><span style="font-family:Menlo;font-size:10.5px">(</span><span style="font-family:Menlo;font-size:10.5px;color:rgb(88,126,168)">C</span><span style="font-family:Menlo;font-size:10.5px">() </span><span style="font-family:Menlo;font-size:10.5px;color:rgb(50,62,125)">as</span><span style="font-family:Menlo;font-size:10.5px"> </span><span style="font-family:Menlo;font-size:10.5px;color:rgb(88,126,168)">C</span><span style="font-family:Menlo;font-size:10.5px">).</span><span style="font-family:Menlo;font-size:10.5px;color:rgb(88,126,168)">f</span><span style="font-family:Menlo;font-size:10.5px">()</span><font face="Menlo" size="1">  </font><font color="#668b49" face="Menlo" size="1">// </font><font color="#668b49" face="Menlo"><font size="1">C_f</font></font></div><div style="margin:0px;font-size:10.5px;line-height:normal;font-family:Menlo">    (<span style="color:rgb(88,126,168)">C</span>() <span style="color:rgb(50,62,125)">as</span> <span style="color:rgb(88,126,168)">P</span>).<span style="color:rgb(88,126,168)">f</span>()<font face="Menlo" size="1">  </font><font color="#668b49" face="Menlo" size="1">// P</font><font color="#668b49" face="Menlo"><font size="1">_f</font></font></div></div></div><div><font color="#668b49" face="Menlo"><font size="1"><br></font></font></div><div>I’d say that if you got confused in the course of explaining how it&#39;s not confusing … well, that’s pretty good evidence that it is indeed confusing.</div><div><br></div><div>Cheers,</div><div><br></div><div>Paul</div></div><div style="word-wrap:break-word"><div><br></div><br><div><blockquote type="cite"><div>On Dec 7, 2015, at 3:56 PM, ilya &lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;ilya.nikokoshev@gmail.com&#39;);" target="_blank">ilya.nikokoshev@gmail.com</a>&gt; wrote:</div><br><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 7, 2015 at 7:17 AM, Paul Cantrell via swift-evolution <span dir="ltr">&lt;<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-evolution@swift.org&#39;);" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">One of the few things in Swift 2 that feels to me like a design flaw is the way Swift mixes static and dynamic method dispatch.<br>
<br>
Alexandros Salazar gives an excellent explanation of this problem — and I agree wholeheartedly with his title for the article:<br>
<br>
    <a href="http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future" rel="noreferrer" target="_blank">http://nomothetis.svbtle.com/the-ghost-of-swift-bugs-future</a><br>
<br>
The upshot is that when we see this:<br>
<br>
    foo.bar()<br>
<br>
…it’s very hard to know how the compiler will determine which implementation of bar() to use. It might use static dispatch; it might use dynamic dispatch.<br>
<br>
The rules that govern this are arcane, and hard to remember. They have the feeling of being a “gotcha” question for job interviews — always a red flag for language features.<br>
<br>
Even if you remember the rules, the information needed to determine whether dispatch is static or dynamic is hard to track down. It depends on whether bar()’s implementation comes from an extension, whether the extension method appeared on the extended protocol, and whether the inferred type of foo is the protocol itself or an implementing type.<br>
<br>
A crucial part of the meaning of “foo.bar()” is implicit, and hard to determine. </blockquote><div><br></div><div>I must admit I&#39;m having difficulty understanding why it&#39;s a big deal whether the dispatch will be static or dynamic. This seems like an implementation detail; any &quot;dynamic dispatch&quot; in the aforementioned sense can actually became static for a final class.</div><div><br></div><div>I understand there can be a confusion about the method called when the protocol contains a method implementation, but there are some simple ways to understand why things work as they do. Just think about function defined as having a &quot;magic prefix&quot; that corresponds technically to vtable where they can be located:</div><div><br></div><div>class C {</div><div>   func C_f // declares C.C_f</div><div>}</div><div><br></div><div>protocol P {</div><div>  func P_f</div><div>}</div><div><br></div><div>// implementation of P.P_f</div><div><br></div><div>extension C:P {</div><div>  // declares that C.C_f = C.P_f</div><div>}</div><div><br></div><div>(C() as C).f -&gt; calls C.C_f = C.P_f</div><div>(C() as P).f -&gt; calls P.P_f</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This runs contrary to Swift’s stated goal of prioritizing clarity at the point of API use, and its general pattern of making intent explicit. And it feels dangerous — a wellspring of insidious bugs.<br>
<br>
Thus:<br>
<br>
<br>
PROPOSAL<br>
<br>
Make the syntax “foo.bar()” always use dynamic dispatch, i.e. always use _only_ the runtime type of foo to determine which implementation of bar() to use. If an extension method collision occurs when a type implements multiple protocols, require the type to explicitly specify which one to use (as Swift already requires the caller to do at the point of invocation).<br>
<br>
<br>
I mean this proposal somewhat as a strawman. It’s such an obvious choice, I’m sure there were good reasons not to do it. But I’d like to propose the obvious solution in order to understand what’s wrong with it. I realize static dispatch precludes some optimizations, but I doubt that this alone drove the design choice. I see no safety or expressiveness upside to the way it works now.<br>
<br>
Cheers,<br>
<br>
Paul<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;swift-evolution@swift.org&#39;);" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div><br></div></div>
</div></blockquote></div><br></div></blockquote></div></div>