<div dir="ltr">So as the <span style="font-size:12.8px"><b>foo&lt;A:P&gt;(_ x:A) </b>function is generic, when we call <b>foo(x) </b>compiler needs to determine what type is <b>A </b>to be able to create concrete function. But x defined as <b>let x = X() as P </b>so we only know about it that it conforms to <b>P </b>but not its real type to put instead of <b>A</b>. Right?<br>But where is the &quot;</span><span style="font-size:12.8px">Protocols do not conform to themselves</span><span style="font-size:12.8px">&quot; limitation is coming out?</span></div><div class="gmail_extra"><br><div class="gmail_quote">2016-12-30 18:25 GMT+07:00 Rien <span dir="ltr">&lt;<a href="mailto:Rien@balancingrock.nl" target="_blank">Rien@balancingrock.nl</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
&gt; On 30 Dec 2016, at 12:14, Mikhail Seriukov via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; Ok,<br>
&gt; But I think I still do not get it.<br>
&gt; What does really happen when we write this?<br>
&gt;&gt; let x = X() as P<br>
&gt;&gt;<br>
<br>
</span>&#39;X()&#39; creates a value.<br>
&#39;as P’ constrains the value such that the only things we know about it is that the value will conform to the protocol P<br>
‘let x =‘ assigns the value to a constant, and the only thing we know about that constant is that we can call an operation of protocol P on it.<br>
<span class="HOEnZb"><font color="#888888"><br>
Rien.<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
&gt; As I said, I expect x to be Any&lt;P&gt; after that. If it is, then it should be ok IMO.<br>
&gt; But if it is not then what is the actual type of x?<br>
&gt;<br>
&gt; So the real question is how the type checker works here?<br>
&gt;<br>
&gt;<br>
&gt; 2016-12-25 22:13 GMT+07:00 Slava Pestov &lt;<a href="mailto:spestov@apple.com">spestov@apple.com</a>&gt;:<br>
&gt;<br>
&gt;&gt; On Dec 22, 2016, at 4:43 PM, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; The following variation works:<br>
&gt;&gt;<br>
&gt;&gt; protocol P {}<br>
&gt;&gt;<br>
&gt;&gt; class P1:P {}<br>
&gt;&gt;<br>
&gt;&gt; class X:P1 {}<br>
&gt;&gt;<br>
&gt;&gt; func foo&lt;A:P&gt;(_ x:A) {}<br>
&gt;&gt;<br>
&gt;&gt; func bar() {<br>
&gt;&gt;     //let x = X() // this compiles<br>
&gt;&gt;     let x = X() as P1 // this does not compile. Why?<br>
&gt;&gt;     foo(x)<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; Which adds credence to the bug theory.<br>
&gt;<br>
&gt; 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.<br>
&gt;<br>
&gt; Slava<br>
&gt;<br>
&gt;&gt;<br>
&gt;&gt; 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.<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;   -- Howard.<br>
&gt;&gt;<br>
&gt;&gt; On 23 December 2016 at 07:29, Kevin Nattinger &lt;<a href="mailto:swift@nattinger.net">swift@nattinger.net</a>&gt; wrote:<br>
&gt;&gt; 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.<br>
&gt;&gt;<br>
&gt;&gt;&gt; On Dec 22, 2016, at 2:17 PM, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; I suspect a compiler bug since A is a P. The equivalent in Java works:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; interface P {}<br>
&gt;&gt;&gt; class X implements P {}<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; &lt;A extends P&gt; void foo(A x) {}<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; void bar() {<br>
&gt;&gt;&gt;     final P x = new X();<br>
&gt;&gt;&gt;     foo(x);<br>
&gt;&gt;&gt; }<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; -- Howard.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On 23 Dec 2016, at 3:19 am, Rien via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; IMO the error message says it all:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Playground execution failed: error: MyPlayground8.playground:9:5: error: cannot invoke &#39;foo&#39; with an argument list of type &#39;(P)&#39;<br>
&gt;&gt;&gt;&gt;    foo(x)<br>
&gt;&gt;&gt;&gt;    ^<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; MyPlayground8.playground:9:5: note: expected an argument list of type &#39;(A)&#39;<br>
&gt;&gt;&gt;&gt;    foo(x)<br>
&gt;&gt;&gt;&gt;    ^<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; I.e. you are passing in a protocol while the function is specified for a type.<br>
&gt;&gt;&gt;&gt; Said other way: On which data do you expect the protocol to operate?<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Regards,<br>
&gt;&gt;&gt;&gt; Rien<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; Site: <a href="http://balancingrock.nl" rel="noreferrer" target="_blank">http://balancingrock.nl</a><br>
&gt;&gt;&gt;&gt; Blog: <a href="http://swiftrien.blogspot.com" rel="noreferrer" target="_blank">http://swiftrien.blogspot.com</a><br>
&gt;&gt;&gt;&gt; Github: <a href="http://github.com/Swiftrien" rel="noreferrer" target="_blank">http://github.com/Swiftrien</a><br>
&gt;&gt;&gt;&gt; Project: <a href="http://swiftfire.nl" rel="noreferrer" target="_blank">http://swiftfire.nl</a><br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; On 22 Dec 2016, at 17:05, Mikhail Seriukov via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; Hello community! I&#39; wondering if somebody can explain this to me.<br>
&gt;&gt;&gt;&gt;&gt; Please take look at the snippet.<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; protocol P {}<br>
&gt;&gt;&gt;&gt;&gt; struct X:P {}<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; func foo&lt;A:P&gt;(_ x:A) {}<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; func bar() {<br>
&gt;&gt;&gt;&gt;&gt;    //let x = X() // this compiles<br>
&gt;&gt;&gt;&gt;&gt;    let x = X() as P // this does not compile. Why?<br>
&gt;&gt;&gt;&gt;&gt;    foo(x)<br>
&gt;&gt;&gt;&gt;&gt; }<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; I expect the both cases to work though. But only first works? And I do not understand why.<br>
&gt;&gt;&gt;&gt;&gt; My coworkers said that it is a compiler bug, but I&#39;m not shure it is.<br>
&gt;&gt;&gt;&gt;&gt; Thanks for the help.<br>
&gt;&gt;&gt;&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt;&gt;&gt;&gt; swift-users mailing list<br>
&gt;&gt;&gt;&gt;&gt; <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
&gt;&gt;&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt;&gt;&gt; swift-users mailing list<br>
&gt;&gt;&gt;&gt; <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
&gt;&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
&gt;&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt;&gt; swift-users mailing list<br>
&gt;&gt;&gt; <a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-users</a><br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; ______________________________<wbr>_________________<br>
&gt;&gt; swift-users mailing list<br>
&gt;&gt; <a href="mailto:swift-users@swift.org">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/<wbr>mailman/listinfo/swift-users</a><br>
&gt;<br>
&gt;<br>
&gt; ______________________________<wbr>_________________<br>
&gt; swift-users mailing list<br>
&gt; <a href="mailto:swift-users@swift.org">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/<wbr>mailman/listinfo/swift-users</a><br>
<br>
</div></div></blockquote></div><br></div>