<div dir="ltr">Ok, <br>But I think I still do not get it.<br>What does really happen when we write this?<br><blockquote type="cite" style="color:rgb(80,0,80);font-size:12.8px"><div dir="ltr"><div 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(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> 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">() </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">as</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(79,129,135)">P<br><br></span></div></div></blockquote>As I said, I expect x to be <span style="font-family:menlo,consolas,"liberation mono",courier,monospace;font-size:13.3333px;background-color:rgb(248,248,248)">Any<P></span> after that. If it is, then it should be ok IMO.<br>But if it is not then what is the actual type of x? <br><br>So the real question is how the type checker works here?<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">2016-12-25 22:13 GMT+07:00 Slava Pestov <span dir="ltr"><<a href="mailto:spestov@apple.com" target="_blank">spestov@apple.com</a>></span>:<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"><br><div><span class=""><blockquote type="cite"><div>On Dec 22, 2016, at 4:43 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_2606981490317391748Apple-interchange-newline"><div><div dir="ltr">The following variation works:<div><br></div><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">protocol</span><span style="font-variant-ligatures:no-common-ligatures"> P {}</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)">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></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)">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></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)">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></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)">func</span><span style="font-variant-ligatures:no-common-ligatures"> bar() {</span></div><div 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"> </span><span style="font-variant-ligatures:no-common-ligatures">//let x = X() // this compiles</span></div><div 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"> </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">let</span><span style="font-variant-ligatures:no-common-ligatures"> 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">() </span><span style="font-variant-ligatures:no-common-ligatures;color:rgb(186,45,162)">as</span><span style="font-variant-ligatures:no-common-ligatures"> </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><span style="font-variant-ligatures:no-common-ligatures">// this does not compile. Why?</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(49,89,93)">foo</span><span style="font-variant-ligatures:no-common-ligatures">(x)</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><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></div></blockquote><div><br></div></span>It’s an intentional limitation. Protocols do not conform to themselves. Lifting the restriction would be difficult to do efficiently given our representation of generics and protocols at runtime.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Slava</div></font></span><div><div class="h5"><div><br><blockquote type="cite"><div><div dir="ltr"><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="m_2606981490317391748gmail_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="m_2606981490317391748h5"><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_2606981490317391748m_-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><div 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_2606981490317391748m_-7294405302096099227webkit-block-placeholder"></div><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><div 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_2606981490317391748m_-7294405302096099227webkit-block-placeholder"></div><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/mailma<wbr>n/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/mailma<wbr>n/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/mailma<wbr>n/listinfo/swift-users</a><br></div></blockquote></div><br></div></div></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>