<div dir="ltr"><div>I&#39;m trying to get my head around the current behavior, but its very hard to understand and remember, and judging by the comments here and on my bug report (SR-6564), so does even people in the core team. It would be nice if someone could present a complete set of rules (all the ones I&#39;ve seen are far to simplified and does not cover all cases).</div><div>Here&#39;s another example to consider:</div><div><br></div><div>protocol P {</div><div>    associatedtype T</div><div>    func f() // *</div><div>}</div><div>extension P {</div><div>    func f() { print(&quot;T is unknown&quot;) }</div><div>}</div><div>extension P where T == Int {</div><div>    func f() { print(&quot;T is Int&quot;) }</div><div>}</div><div><br></div><div>struct X&lt;T&gt; : P {</div><div>    func f() { print(&quot;this one is actually best&quot;) }</div><div>}</div><div>extension X where T == Int {</div><div>    func f() { print(&quot;this one is actually better than best.&quot;) }</div><div>}</div><div><br></div><div>struct Y&lt;U&gt; where U: P, U.T == Int {</div><div>    typealias T = U.T</div><div>    var a: U</div><div>    func g() { a.f() }</div><div>}</div><div><br></div><div>let x = X&lt;Int&gt;()</div><div>x.f() // What will this print?</div><div><br></div><div>let y = Y(a: X&lt;Int&gt;())</div><div>y.g() // What will this print?</div><div><br></div><div>If anyone knows for sure what this program will print (without having to run it), please enlighten me!</div><div><br></div><div>/Jens</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Dec 9, 2017 at 1:54 AM, Jordan Rose <span dir="ltr">&lt;<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div>Consider this example:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><span class=""><div><div>protocol P {</div></div><div><div>    associatedtype T</div></div><div><div>    func f() // *</div></div><div><div>}</div></div><div><div>extension P {</div></div><div><div>    func f() { print(&quot;T is unknown&quot;) }</div></div><div><div>}</div></div><div><div>extension P where T == Int {</div></div><div><div>    func f() { print(&quot;T is Int&quot;) }</div></div><div><div>}</div></div><div><div><br></div></div><div><div>struct X&lt;T&gt; : P {</div></div></span><div><div>    func f() { print(&quot;this one is actually best&quot;) }</div></div><span class=""><div><div>}</div></div><div><div><br></div></div><div><div>struct Y&lt;U&gt; where U: P, U.T == Int {</div></div></span><span class=""><div><div>    typealias T = U.T</div></div><div><div>    var a: U</div></div><div><div>    func g() { a.f() }</div></div><div><div>}</div></div><div><div><br></div></div></span><span class=""><div><div>let x = X&lt;Int&gt;()</div></div></span><div><div>x.f() // &quot;this one is actually best&quot;</div></div><span class=""><div><div><br></div></div><div><div>let y = Y(a: X&lt;Int&gt;())</div></div></span><div><div>y.g() // &quot;this one is actually best&quot;</div></div></blockquote><div><div><br></div></div><div>I can&#39;t think of any other choice for &#39;a.f()&#39; that would preserve this behavior, which means we&#39;re doing the best thing: prefer dynamic dispatch over static dispatch when operating on a generic value. (Or at least the &quot;least bad&quot; thing.) And of course this reasoning has to be local; Y.g can&#39;t look and say &quot;oh, I know nothing else conforms to P, and X {does, doesn&#39;t} have a custom implementation, so I should pick the constrained extension instead&quot;.</div><div><br></div><div>The real answer might be &quot;we should have had a different syntax for default implementations vs. mixin operations&quot;, but that&#39;s a much bigger can of worms.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Jordan</div></font></span><div><div class="h5"><br><div><br><blockquote type="cite"><div>On Dec 8, 2017, at 13:07, Jens Persson via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_3843216022228456842Apple-interchange-newline"><div><div dir="ltr"><div>Thanks Slava and Greg,</div><div><br></div><div>(</div>I&#39;m aware that it prints &quot;T is Int&quot; from both calls if I remove func f() from P itself, that&#39;s why I wrote &quot;... <span style="font-size:12.800000190734863px">unless * is commented out.&quot; in</span> the comment of the last line<div><span style="font-size:12.800000190734863px">Note that the &quot;U.T == Int&quot;-part of </span><br></div><div><span style="font-size:12.800000190734863px">  struct Y&lt;U&gt; where U: P, U.T == Int {</span></div><div><span style="font-size:12.800000190734863px">is key here. If it had been only</span></div><div><span style="font-size:12.800000190734863px">  struct Y&lt;U&gt; where U: P {</span><span style="font-size:12.800000190734863px"><br></span></div><div><span style="font-size:12.800000190734863px">then I hadn&#39;t been surprised that it printed &quot;T is unknown&quot;.</span></div><div><span style="font-size:12.800000190734863px">)</span></div><div><span style="font-size:12.800000190734863px"><br></span></div><div><span style="font-size:12.800000190734863px">Filed </span><a href="https://bugs.swift.org/browse/SR-6564" target="_blank">https://bugs.swift.org/<wbr>browse/SR-6564</a><span style="font-size:12.800000190734863px"> since I think it is just strange that the compiler should not use its knowledge of U.T == Int when choosing between the two f()-implementations.</span></div><div><span style="font-size:12.800000190734863px">I think I will be a little disappointed if the solution is to deem it an ambiguity</span></div><div><span style="font-size:12.800000190734863px">: )</span></div><div><span style="font-size:12.800000190734863px"><br></span></div><div>/Jens</div><div><span style="font-size:12.800000190734863px"><br></span></div><div><span style="font-size:12.800000190734863px"><br></span></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 8, 2017 at 9:19 PM, Greg Parker <span dir="ltr">&lt;<a href="mailto:gparker@apple.com" target="_blank">gparker@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Evidence in favor of Slava&#39;s analysis: if you remove `func f()` from P itself, leaving it in the extensions only, then you get &quot;T is Int&quot; from both calls.<br>
<div class="m_3843216022228456842HOEnZb"><div class="m_3843216022228456842h5"><br>
<br>
&gt; On Dec 8, 2017, at 12:12 PM, Slava Pestov via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; Hi Jens,<br>
&gt;<br>
&gt; I think the problem is that overload ranking always prefers a protocol requirement to a protocol extension member, because usually you want the dynamic dispatch through the requirement instead of calling the default implementation. But it appears that this heuristic does not take into account the fact that the protocol extension member could be more constrained than the requirement.<br>
&gt;<br>
&gt; Please file a bug, but it is unclear what the desired behavior actually is here. Perhaps it should just diagnose an ambiguity.<br>
&gt;<br>
&gt; Slava<br>
&gt;<br>
&gt;&gt; On Dec 8, 2017, at 6:25 AM, Jens Persson via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; Hi all!<br>
&gt;&gt;<br>
&gt;&gt; Can someone please explain the rationale behind the last line printing<br>
&gt;&gt; &quot;T is unknown&quot;<br>
&gt;&gt; rather than (what I would expect):<br>
&gt;&gt; &quot;T is Int&quot;<br>
&gt;&gt; in the following program?<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; protocol P {<br>
&gt;&gt;    associatedtype T<br>
&gt;&gt;    func f() // *<br>
&gt;&gt; }<br>
&gt;&gt; extension P {<br>
&gt;&gt;    func f() { print(&quot;T is unknown&quot;) }<br>
&gt;&gt; }<br>
&gt;&gt; extension P where T == Int {<br>
&gt;&gt;    func f() { print(&quot;T is Int&quot;) }<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; struct X&lt;T&gt; : P {}<br>
&gt;&gt;<br>
&gt;&gt; struct Y&lt;U&gt; where U: P, U.T == Int {<br>
&gt;&gt;    // NOTE: The compiler/type-checker knows that U.T == Int here so ...<br>
&gt;&gt;    typealias T = U.T<br>
&gt;&gt;    var a: U<br>
&gt;&gt;    func g() { a.f() } // ... how/why could this print anything but &quot;T is Int&quot;?<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; let x = X&lt;Int&gt;()<br>
&gt;&gt; x.f() // Prints &quot;T is Int&quot;, no matter if * is commented out or not.<br>
&gt;&gt;<br>
&gt;&gt; let y = Y(a: X&lt;Int&gt;())<br>
&gt;&gt; y.g() // Prints &quot;T is unknown&quot; unless * is commented out. Why?<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; IMHO this looks like the compiler simply ignores that struct Y&lt;U&gt; has the constraint  U.T == Int.<br>
&gt;&gt; How else to explain this behavior?<br>
&gt;&gt; /Jens<br>
&gt;&gt;<br>
&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt; swift-users mailing list<br>
&gt;&gt; <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
&gt;<br>
&gt; ______________________________<wbr>_________________<br>
&gt; swift-users mailing list<br>
&gt; <a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-users</a><br>
<br>
</div></div></blockquote></div><br></div>
______________________________<wbr>_________________<br>swift-users mailing list<br><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></div></blockquote></div><br></div>