<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">When you call foo&lt;A : P&gt;(_: A) with a concrete type C, the type checker performs a conformance check of C to P. If C is a protocol type, the conformance check fails. Ie, unlike the subtype relation (roughly, “I can assign a value of this type to that”), the conformance relation (“I can substitute this for a generic parameter conforming to”) is not reflexive.</div><div class=""><br class=""></div><div class="">Swift compiles generic code in a way where values of generic type have the most efficient representation in memory — you pay a CPU cost because they’re still manipulated indirectly (unlike C++ templates, which are cloned and inlined at compile time), but there’s no representational cost as in Java’s erased generics for example.</div><div class=""><br class=""></div><div class="">What this means is that all types that you can substitute for a generic parameter have to be interchangeable in some basic sense.</div><div class=""><br class=""></div><div class="">The main problem is with class-bound protocols:</div><div class=""><br class=""></div><div class="">protocol CP : class {</div><div class="">&nbsp; func foo()</div><div class="">&nbsp; func bar()</div><div class="">}</div><div class=""><br class=""></div><div class="">And a function taking an array of CP’s:</div><div class=""><br class=""></div><div class="">func foo&lt;T : CP&gt;(t: [T]) {}</div><div class=""><br class=""></div><div class="">All concrete types that conform to a class-bound protocol have a single retainable-pointer representation, so the function takes an array of pointers.</div><div class=""><br class=""></div><div class="">However, a value of protocol type ‘CP’ is represented as the underlying concrete value — a pointer to a class instance conforming to CP — together with the conformance table that stores the function pointers for the implementations of ‘foo’ and ‘bar’.</div><div class=""><br class=""></div><div class="">So unlike in Java, where method implementations for an interface are stored “inside” the instance, in Swift protocol requirement implementations are always passed ‘on the side’.</div><div class=""><br class=""></div><div class="">With opaque protocols, a type parameter has variable size, so there’s no representational issue. Instead you just get a double indirection.</div><div class=""><br class=""></div><div class="">Say you have</div><div class=""><br class=""></div><div class="">protocol P {}</div><div class="">func foo&lt;T : P&gt;(t: [T]) {}</div><div class=""><br class=""></div><div class="">If you call foo with the substitution P := P, then you’re passing in an array of values *where each value has its own conformance to P*. But foo() itself wants the conformance P : P. So when you call a protocol requirement on an element of ’t’. you will first call a method in the ‘dummy’ conformance P : P, which will unpack the “real” concrete type from the value of type P and call the right method on that.</div><div class=""><br class=""></div><div class="">So we could implement self-conformance for non-class-bound protocols. For class-bound protocols, I’m not sure how to do it efficiently.</div><div class=""><br class=""></div><div class="">Slava</div><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 30, 2016, at 8:26 AM, Mikhail Seriukov &lt;<a href="mailto:zloisop@gmail.com" class="">zloisop@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">So as the&nbsp;<span style="font-size:12.8px" class=""><b class="">foo&lt;A:P&gt;(_ x:A) </b>function is generic, when we call <b class="">foo(x) </b>compiler needs to determine what type is <b class="">A </b>to be able to create concrete function. But x defined as <b class="">let x = X() as P </b>so we only know about it that it conforms to <b class="">P </b>but not its real type to put instead of <b class="">A</b>. Right?<br class="">But where is the "</span><span style="font-size:12.8px" class="">Protocols do not conform to themselves</span><span style="font-size:12.8px" class="">" limitation is coming out?</span></div><div class="gmail_extra"><br class=""><div class="gmail_quote">2016-12-30 18:25 GMT+07:00 Rien <span dir="ltr" class="">&lt;<a href="mailto:Rien@balancingrock.nl" target="_blank" class="">Rien@balancingrock.nl</a>&gt;</span>:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br class="">
&gt; On 30 Dec 2016, at 12:14, Mikhail Seriukov via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class="">
&gt;<br class="">
&gt; Ok,<br class="">
&gt; But I think I still do not get it.<br class="">
&gt; What does really happen when we write this?<br class="">
&gt;&gt; let x = X() as P<br class="">
&gt;&gt;<br class="">
<br class="">
</span>'X()' creates a value.<br class="">
'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 class="">
‘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 class="">
<span class="HOEnZb"><font color="#888888" class=""><br class="">
Rien.<br class="">
</font></span><div class="HOEnZb"><div class="h5"><br class="">
&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 class="">
&gt; But if it is not then what is the actual type of x?<br class="">
&gt;<br class="">
&gt; So the real question is how the type checker works here?<br class="">
&gt;<br class="">
&gt;<br class="">
&gt; 2016-12-25 22:13 GMT+07:00 Slava Pestov &lt;<a href="mailto:spestov@apple.com" class="">spestov@apple.com</a>&gt;:<br class="">
&gt;<br class="">
&gt;&gt; On Dec 22, 2016, at 4:43 PM, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class="">
&gt;&gt;<br class="">
&gt;&gt; The following variation works:<br class="">
&gt;&gt;<br class="">
&gt;&gt; protocol P {}<br class="">
&gt;&gt;<br class="">
&gt;&gt; class P1:P {}<br class="">
&gt;&gt;<br class="">
&gt;&gt; class X:P1 {}<br class="">
&gt;&gt;<br class="">
&gt;&gt; func foo&lt;A:P&gt;(_ x:A) {}<br class="">
&gt;&gt;<br class="">
&gt;&gt; func bar() {<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp;//let x = X() // this compiles<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp;let x = X() as P1 // this does not compile. Why?<br class="">
&gt;&gt;&nbsp; &nbsp; &nbsp;foo(x)<br class="">
&gt;&gt; }<br class="">
&gt;&gt;<br class="">
&gt;&gt; Which adds credence to the bug theory.<br class="">
&gt;<br class="">
&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 class="">
&gt;<br class="">
&gt; Slava<br class="">
&gt;<br class="">
&gt;&gt;<br class="">
&gt;&gt; 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.<br class="">
&gt;&gt;<br class="">
&gt;&gt;<br class="">
&gt;&gt;&nbsp; &nbsp;-- Howard.<br class="">
&gt;&gt;<br class="">
&gt;&gt; On 23 December 2016 at 07:29, Kevin Nattinger &lt;<a href="mailto:swift@nattinger.net" class="">swift@nattinger.net</a>&gt; wrote:<br class="">
&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 class="">
&gt;&gt;<br class="">
&gt;&gt;&gt; On Dec 22, 2016, at 2:17 PM, Howard Lovatt via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; I suspect a compiler bug since A is a P. The equivalent in Java works:<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; interface P {}<br class="">
&gt;&gt;&gt; class X implements P {}<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; &lt;A extends P&gt; void foo(A x) {}<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; void bar() {<br class="">
&gt;&gt;&gt;&nbsp; &nbsp; &nbsp;final P x = new X();<br class="">
&gt;&gt;&gt;&nbsp; &nbsp; &nbsp;foo(x);<br class="">
&gt;&gt;&gt; }<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; -- Howard.<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt; On 23 Dec 2016, at 3:19 am, Rien via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:<br class="">
&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; IMO the error message says it all:<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; Playground execution failed: error: MyPlayground8.playground:9:5: error: cannot invoke 'foo' with an argument list of type '(P)'<br class="">
&gt;&gt;&gt;&gt;&nbsp; &nbsp; foo(x)<br class="">
&gt;&gt;&gt;&gt;&nbsp; &nbsp; ^<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; MyPlayground8.playground:9:5: note: expected an argument list of type '(A)'<br class="">
&gt;&gt;&gt;&gt;&nbsp; &nbsp; foo(x)<br class="">
&gt;&gt;&gt;&gt;&nbsp; &nbsp; ^<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; I.e. you are passing in a protocol while the function is specified for a type.<br class="">
&gt;&gt;&gt;&gt; Said other way: On which data do you expect the protocol to operate?<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; Regards,<br class="">
&gt;&gt;&gt;&gt; Rien<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; Site: <a href="http://balancingrock.nl/" rel="noreferrer" target="_blank" class="">http://balancingrock.nl</a><br class="">
&gt;&gt;&gt;&gt; Blog: <a href="http://swiftrien.blogspot.com/" rel="noreferrer" target="_blank" class="">http://swiftrien.blogspot.com</a><br class="">
&gt;&gt;&gt;&gt; Github: <a href="http://github.com/Swiftrien" rel="noreferrer" target="_blank" class="">http://github.com/Swiftrien</a><br class="">
&gt;&gt;&gt;&gt; Project: <a href="http://swiftfire.nl/" rel="noreferrer" target="_blank" class="">http://swiftfire.nl</a><br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;<br class="">
&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" class="">swift-users@swift.org</a>&gt; wrote:<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; Hello community! I' wondering if somebody can explain this to me.<br class="">
&gt;&gt;&gt;&gt;&gt; Please take look at the snippet.<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; protocol P {}<br class="">
&gt;&gt;&gt;&gt;&gt; struct X:P {}<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; func foo&lt;A:P&gt;(_ x:A) {}<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; func bar() {<br class="">
&gt;&gt;&gt;&gt;&gt;&nbsp; &nbsp; //let x = X() // this compiles<br class="">
&gt;&gt;&gt;&gt;&gt;&nbsp; &nbsp; let x = X() as P // this does not compile. Why?<br class="">
&gt;&gt;&gt;&gt;&gt;&nbsp; &nbsp; foo(x)<br class="">
&gt;&gt;&gt;&gt;&gt; }<br class="">
&gt;&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt;&gt; I expect the both cases to work though. But only first works? And I do not understand why.<br class="">
&gt;&gt;&gt;&gt;&gt; My coworkers said that it is a compiler bug, but I'm not shure it is.<br class="">
&gt;&gt;&gt;&gt;&gt; Thanks for the help.<br class="">
&gt;&gt;&gt;&gt;&gt; ______________________________<wbr class="">_________________<br class="">
&gt;&gt;&gt;&gt;&gt; swift-users mailing list<br class="">
&gt;&gt;&gt;&gt;&gt; <a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
&gt;&gt;&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
&gt;&gt;&gt;&gt;<br class="">
&gt;&gt;&gt;&gt; ______________________________<wbr class="">_________________<br class="">
&gt;&gt;&gt;&gt; swift-users mailing list<br class="">
&gt;&gt;&gt;&gt; <a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
&gt;&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
&gt;&gt;&gt; ______________________________<wbr class="">_________________<br class="">
&gt;&gt;&gt; swift-users mailing list<br class="">
&gt;&gt;&gt; <a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
&gt;&gt;<br class="">
&gt;&gt;<br class="">
&gt;&gt; ______________________________<wbr class="">_________________<br class="">
&gt;&gt; swift-users mailing list<br class="">
&gt;&gt; <a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
&gt;<br class="">
&gt;<br class="">
&gt; ______________________________<wbr class="">_________________<br class="">
&gt; swift-users mailing list<br class="">
&gt; <a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-users</a><br class="">
<br class="">
</div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></body></html>