<div dir="ltr">On Sun, Mar 12, 2017 at 1:39 AM, Karl Wagner <span dir="ltr"><<a href="mailto:razielim@gmail.com" target="_blank">razielim@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><span class="gmail-"><br><div><blockquote type="cite"><div>On 12 Mar 2017, at 08:21, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>> wrote:</div><br class="gmail-m_-5255648766755841190Apple-interchange-newline"><div><div dir="ltr">Sorry, I'm confused. The following works:<div><br></div><div>```</div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">protocol</span><span style="font-variant-ligatures:no-common-ligatures"> Promise {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(186,45,162)"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures">associatedtype</span><span style="font-variant-ligatures:no-common-ligatures"> Result</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">protocol</span><span style="font-variant-ligatures:no-common-ligatures"> Scanner {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">associatedtype</span><span style="font-variant-ligatures:no-common-ligatures"> ScannerPromise : Promise</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> frobnicate<T>(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">_</span><span style="font-variant-ligatures:no-common-ligatures">: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">T</span><span style="font-variant-ligatures:no-common-ligatures">) -> ScannerPromise</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">where</span><span style="font-variant-ligatures:no-common-ligatures"> ScannerPromise.Result == </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">T</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div>```</div><div><br></div><div>Why does it matter if `ScannerPromise` is generic or not?</div><div class="gmail_extra"><br></div></div></div></blockquote></div><br></span><div>That’s some pretty strange syntax. I admit I didn’t even try that. ScannerPromise would necessarily have to be generic in this context, because I want to bind one of its associated types to a generic parameter from a function. There is no way a non-generic type could do that.</div><div><br></div><div>That said, even though the compiler accepts your code, it doesn’t seem to actually work:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><font face="Courier">protocol Promise {</font></div></div><div><div><font face="Courier"> associatedtype Result</font></div></div><div><div><font face="Courier"> func await() -> Result</font></div></div><span class="gmail-"><div><div><font face="Courier">}</font></div></div><div><div><font face="Courier"><br></font></div></div><div><div><font face="Courier">protocol Scanner {</font></div></div><div><div><font face="Courier"> associatedtype ScannerPromise : Promise</font></div></div><div><div><font face="Courier"> func frobnicate<T>(_: T) -> ScannerPromise</font></div></div><div><div><font face="Courier"> where ScannerPromise.Result == T</font></div></div><div><div><font face="Courier">}</font></div></div><div><div><font face="Courier"><br></font></div></div></span><div><div><font face="Courier">func use<S: Scanner, T>(_ s: S, _ t: T) -> T {</font></div></div><div><div><font face="Courier"><span class="gmail-m_-5255648766755841190Apple-tab-span" style="white-space:pre-wrap">        </span>return s.frobnicate(t).await()</font></div></div><div><div><font face="Courier">}</font></div></div></blockquote><div><div><span class="gmail-m_-5255648766755841190Apple-tab-span" style="white-space:pre-wrap">        </span> </div></div><div><br></div><div>3.0.2: Segfault</div><div><br></div><div>3.1:</div><div><br></div><div>error: repl.swift:13:14: error: cannot invoke 'frobnicate' with an argument list of type '(T)'<br> return s.frobnicate(t).await()<br> ^<br><br>repl.swift:13:14: note: expected an argument list of type '(T)'<br> return s.frobnicate(t).await()</div></div></blockquote><div><br></div><div>That's because your `T` in `use` has no relationship with your `T` in `protocol Scanner`; you just happen to have chosen the same letter of the alphabet. This becomes clear if you rename:</div><div><br></div><div>```</div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> use<S: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Scanner</span><span style="font-variant-ligatures:no-common-ligatures">, U>(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> s: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">S</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> t: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">U</span><span style="font-variant-ligatures:no-common-ligatures">) -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">U</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">return</span><span style="font-variant-ligatures:no-common-ligatures"> s.frobnicate(t).await()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">}</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><br></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"><b>// cannot invoke 'frobnicate' with an argument list of type '(U)'</b></span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><b>// expected an argument list of type '(T)'</b><br></p></div><div>```</div><div><br></div><div>However, this works:</div><div><br></div><div>```</div><div>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> use<S: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Scanner</span><span style="font-variant-ligatures:no-common-ligatures">, T>(</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> s: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">S</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> t: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">T</span><span style="font-variant-ligatures:no-common-ligatures">) -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">T</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">where</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">S</span><span style="font-variant-ligatures:no-common-ligatures">.ScannerPromise.Result == </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">T</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(49,89,93)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">return</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> s.</span><span style="font-variant-ligatures:no-common-ligatures">frobnicate</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(t).</span><span style="font-variant-ligatures:no-common-ligatures">await</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">}</span></p></div><div>```</div><div><br></div><div>...or just this:</div><div><br></div><div>```</div><div><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">func</span><span style="font-variant-ligatures:no-common-ligatures"> use<S: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">Scanner</span><span style="font-variant-ligatures:no-common-ligatures">>(</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)"> _</span><span style="font-variant-ligatures:no-common-ligatures"> s: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">S</span><span style="font-variant-ligatures:no-common-ligatures">, </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">_</span><span style="font-variant-ligatures:no-common-ligatures"> t: </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">S</span><span style="font-variant-ligatures:no-common-ligatures">.ScannerPromise.Result</span></p><p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">) -> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">S</span><span style="font-variant-ligatures:no-common-ligatures">.ScannerPromise.Result {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(49,89,93)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">return</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> s.</span><span style="font-variant-ligatures:no-common-ligatures">frobnicate</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">(t).</span><span style="font-variant-ligatures:no-common-ligatures">await</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">}</span></p></div><div>```</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><span class="gmail-HOEnZb"><font color="#888888"><div></div><div>- Karl</div></font></span></div></blockquote></div><br></div></div>