<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="font-size:12.8px">No it does not.</div><div style="font-size:12.8px">You have made a type out of the parameter. It’s no longer a protocol.</div><div style="font-size:12.8px">IMO the failure here is to understand the difference between a type and a protocol.</div><div style="font-size:12.8px">A type (even if empty) is always a combination of storage with functions (that are assumed to work on the data in storage)</div><div style="font-size:12.8px">A protocol is just a definition of functions without the accompanying data.</div></blockquote><div><br>I see your point. <br>But actually when I write it as  `<span style="font-size:12.8px">let x = X() as P` I really mean that I want `x` to be `AnyObject` but conforming to P, not just protocol itself.<br>Is it even possible to downcast it this way?</span></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-12-23 14:51 GMT+07:00 Marinus van der Lugt <span dir="ltr">&lt;<a href="mailto:rien@starbase55.com" target="_blank">rien@starbase55.com</a>&gt;</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 22 Dec 2016, at 22:43, Howard Lovatt &lt;<a href="mailto:howard.lovatt@gmail.com" target="_blank">howard.lovatt@gmail.com</a>&gt; wrote:</div><br class="m_3726349150314027147Apple-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&lt;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">&gt;(</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><div><br></div></span>No it does not.</div><div>You have made a type out of the parameter. It’s no longer a protocol.</div><div>IMO the failure here is to understand the difference between a type and a protocol.</div><div>A type (even if empty) is always a combination of storage with functions (that are assumed to work on the data in storage)</div><div>A protocol is just a definition of functions without the accompanying data.</div><div><br></div><div>Rien.</div><div><div class="h5"><div><br></div><div><br></div><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&#39;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_3726349150314027147gmail_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">&lt;<a href="mailto:swift@nattinger.net" target="_blank">swift@nattinger.net</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">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_3726349150314027147h5"><div><br><div><blockquote type="cite"><div>On Dec 22, 2016, at 2:17 PM, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</div><br class="m_3726349150314027147m_-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_3726349150314027147m_-7294405302096099227webkit-block-placeholder"></div><div style="margin:0px;font-size:12px;line-height:normal;font-family:Helvetica"><span style="font-size:12pt">&lt;A extends P&gt; 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_3726349150314027147m_-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 &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; 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 &#39;foo&#39; with an argument list of type &#39;(P)&#39;</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 &#39;(A)&#39;</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 &lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt; wrote:</span><br></blockquote><blockquote type="cite"><span></span><br></blockquote><blockquote type="cite"><span>Hello community! I&#39; 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&lt;A:P&gt;(_ 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&#39;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>
</div></blockquote></div><br></div></div></div></blockquote></div><br></div>