<div dir="ltr">The following variation works:<div><br></div><div><p 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">protocol</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> P {}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,0,0);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></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)">class</span><span style="font-variant-ligatures:no-common-ligatures"> P1:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">P</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);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></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)">class</span><span style="font-variant-ligatures:no-common-ligatures"> X:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">P1</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);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></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)">func</span><span style="font-variant-ligatures:no-common-ligatures"> foo<A:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">P</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"> x:</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">A</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);min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></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)">func</span><span style="font-variant-ligatures:no-common-ligatures"> bar() {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">//let x = X() // this compiles</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:menlo;color:rgb(0,132,0)"><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)">let</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> x = </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">X</span><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)">as</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">P1</span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)"> </span><span style="font-variant-ligatures:no-common-ligatures">// this does not compile. Why?</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(49,89,93)">foo</span><span style="font-variant-ligatures:no-common-ligatures">(x)</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><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures">Which adds credence to the bug theory.</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures">Note two changes: 1. two levels of inheritance and 2. change to classes. If you do two levels using protocols it doesn't work if you use either classes or structs.</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"> -- Howard.<br></div></div>
<br><div class="gmail_quote">On 23 December 2016 at 07:29, Kevin Nattinger <span dir="ltr"><<a href="mailto:swift@nattinger.net" target="_blank">swift@nattinger.net</a>></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">I recall seeing a request on the -evolution list for something like `T := X` to indicate it could be X itself or anything inheriting / implementing it, so it’s certainly known behavior, if not desired. IMO it’s a bug and `:` should be fixed to include the root type, whether or not that requires a discussion on -evolution.<div><div class="h5"><div><br><div><blockquote type="cite"><div>On Dec 22, 2016, at 2:17 PM, Howard Lovatt via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</div><br class="m_-7294405302096099227Apple-interchange-newline"><div><div dir="auto"><div>I suspect a compiler bug since A is a P. The equivalent in Java works:</div><div><br></div><div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica"><span style="font-size:12pt">interface P {}</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica"><span style="font-size:12pt">class X implements P {}</span></div><p style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica;min-height:13.8px"><span style="font-size:12pt"> </span><br class="m_-7294405302096099227webkit-block-placeholder"></p><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica"><span style="font-size:12pt"><A extends P> void foo(A x) {}</span></div><p style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica;min-height:13.8px"><span style="font-size:12pt"> </span><br class="m_-7294405302096099227webkit-block-placeholder"></p><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica"><span style="font-size:12pt">void bar() {</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica"><span style="font-size:12pt"> final P x = new X();</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica"><span style="font-size:12pt"> foo(x);</span></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica"><span style="font-size:12pt">}</span></div><br>-- Howard. </div><div><br>On 23 Dec 2016, at 3:19 am, Rien via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><span>IMO the error message says it all:</span><br><span></span><br><span>Playground execution failed: error: MyPlayground8.playground:9:5: error: cannot invoke 'foo' with an argument list of type '(P)'</span><br><span> foo(x)</span><br><span> ^</span><br><span></span><br><span>MyPlayground8.playground:9:5: note: expected an argument list of type '(A)'</span><br><span> foo(x)</span><br><span> ^</span><br><span></span><br><span>I.e. you are passing in a protocol while the function is specified for a type.</span><br><span>Said other way: On which data do you expect the protocol to operate?</span><br><span></span><br><span>Regards,</span><br><span>Rien</span><br><span></span><br><span>Site: <a href="http://balancingrock.nl/" target="_blank">http://balancingrock.nl</a></span><br><span>Blog: <a href="http://swiftrien.blogspot.com/" target="_blank">http://swiftrien.blogspot.com</a></span><br><span>Github: <a href="http://github.com/Swiftrien" target="_blank">http://github.com/Swiftrien</a></span><br><span>Project: <a href="http://swiftfire.nl/" target="_blank">http://swiftfire.nl</a></span><br><span></span><br><span></span><br><span></span><br><span></span><br><blockquote type="cite"><span>On 22 Dec 2016, at 17:05, Mikhail Seriukov via swift-users <<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>> wrote:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Hello community! I' wondering if somebody can explain this to me.</span><br></blockquote><blockquote type="cite"><span>Please take look at the snippet.</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>protocol P {}</span><br></blockquote><blockquote type="cite"><span>struct X:P {}</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>func foo<A:P>(_ x:A) {}</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>func bar() {</span><br></blockquote><blockquote type="cite"><span> //let x = X() // this compiles</span><br></blockquote><blockquote type="cite"><span> let x = X() as P // this does not compile. Why?</span><br></blockquote><blockquote type="cite"><span> foo(x)</span><br></blockquote><blockquote type="cite"><span>}</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>I expect the both cases to work though. But only first works? And I do not understand why.</span><br></blockquote><blockquote type="cite"><span>My coworkers said that it is a compiler bug, but I'm not shure it is.</span><br></blockquote><blockquote type="cite"><span>Thanks for the help.</span><br></blockquote><blockquote type="cite"><span>______________________________<wbr>_________________</span><br></blockquote><blockquote type="cite"><span>swift-users mailing list</span><br></blockquote><blockquote type="cite"><span><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a></span><br></blockquote><blockquote type="cite"><span><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a></span><br></blockquote><span></span><br><span>______________________________<wbr>_________________</span><br><span>swift-users mailing list</span><br><span><a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-users" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a></span><br></div></blockquote></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></div></blockquote></div><br></div>