<div dir="ltr">There are a couple of reasons why it&#39;s not possible.<div><br></div><div>- You can&#39;t force A and B to be protocols (forcing them to be protocols is distinct from forcing them to be types that implement a certain protocol). If you pass in two concrete types &quot;Any&lt;A, B&gt;&quot; becomes invalid.</div><div><br></div><div>- If a generic type has any constraints on it, that generic type cannot be fulfilled by an existential today. A protocol is not considered to implement itself.</div><div><br></div><div>Changing how generics work to accommodate this functionality is beyond the scope of this proposal, which is already too big as is.</div><div><br></div><div>Austin</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 28, 2016 at 12:31 PM, Thorsten Seitz <span dir="ltr">&lt;<a href="mailto:tseitz42@icloud.com" target="_blank">tseitz42@icloud.com</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"><div>I’m not happy with that restriction in the proposal:</div><div><br></div><p style="margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)">Existentials cannot be used with generics in the following ways:</p><ul style="padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(51,51,51);font-family:&#39;Helvetica Neue&#39;,Helvetica,&#39;Segoe UI&#39;,Arial,freesans,sans-serif,&#39;Apple Color Emoji&#39;,&#39;Segoe UI Emoji&#39;,&#39;Segoe UI Symbol&#39;;font-size:16px;background-color:rgb(255,255,255)"><li><p style="margin-top:16px;margin-bottom:16px">In generic declarations, with the requirements composed out of generic type variables:</p><div style="margin-bottom:16px"><pre style="font-family:Consolas,&#39;Liberation Mono&#39;,Menlo,Courier,monospace;font-size:14px;margin-top:0px;margin-bottom:0px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span style="color:rgb(150,152,150)">// NOT ALLOWED</span>
<span style="color:rgb(167,29,93)">func</span> <span style="color:rgb(121,93,163)">foo</span>&lt;A, B&gt;(x: A, y: B) <span style="color:rgb(167,29,93)">-&gt;</span> <span style="color:rgb(0,134,179)">Any</span>&lt;A, B&gt; { <span style="color:rgb(167,29,93)">...</span> }</pre></div></li></ul><div><br></div><div>Why is that not allowed?</div><div><br></div><div>I would have hoped to be able to write something like</div><div><br></div><div>func union&lt;A, B&gt;(x: Set&lt;A&gt;, y: Set&lt;B&gt;) -&gt; Set&lt;Any&lt;A, B&gt;&gt; { … }</div><span class="HOEnZb"><font color="#888888"><div><br></div><div><br></div><div>-Thorsten</div><div><br></div><div><br></div><div><br></div></font></span><div><blockquote type="cite"><div><div class="h5"><div>Am 26.05.2016 um 07:53 schrieb Austin Zheng via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;:</div><br></div></div><div><div><div class="h5"><div dir="ltr">The inimitable Joe Groff provided me with an outline as to how the design could be improved. I&#39;ve taken the liberty of rewriting parts of the proposal to account for his advice.<div><br></div><div>It turns out the runtime type system is considerably more powerful than I expected. The previous concept in which protocols with associated types&#39; APIs were vended out selectively and using existentials has been discarded.</div><div><br></div><div>Instead, all the associated types that belong to an existential are accessible as &#39;anonymous&#39; types within the scope of the existential. These anonymous types are not existentials - they are an anonymous representation of whatever concrete type is satisfying the existential&#39;s value&#39;s underlying type&#39;s associated type.</div><div><br></div><div>This is an enormous step up in power - for example, an existential can return a value of one of these anonymous associated types from one function and pass it into another function that takes the same type, maintaining perfect type safety but without ever revealing the actual type. There is no need anymore to limit the APIs exposed to the user, although there may still exist APIs that are semantically useless without additional type information.</div><div><br></div><div>A set of conversions has also been defined. At compile-time &#39;as&#39; can be used to turn values of these anonymous associated types back into existentials based on the constraints defined earlier. &#39;as?&#39; can also be used for conditional casting of these anonymously-typed values into potential actual types.</div><div><br></div><div>As always, the link is here, and feedback would be greatly appreciated: <a href="https://github.com/austinzheng/swift-evolution/blob/az-existentials/proposals/XXXX-enhanced-existentials.md" target="_blank">https://github.com/austinzheng/swift-evolution/blob/az-existentials/proposals/XXXX-enhanced-existentials.md</a></div><div><br></div><div>Best,</div><div>Austin</div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Tue, May 24, 2016 at 5:09 AM, Matthew Johnson via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><br>
<br>
Sent from my iPad<br>
<span><br>
On May 23, 2016, at 9:52 PM, Brent Royal-Gordon via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
<br>
&gt;&gt; One initial bit of feedback -  I believe if you have existential types, I believe you can define Sequence Element directly, rather than with a type alias. e.g.<br>
&gt;&gt;<br>
&gt;&gt; protocol Sequence {<br>
&gt;&gt;  associatedtype Element<br>
&gt;&gt;  associatedtype Iterator: any&lt;IteratorProtocol where IteratorProtocol.Element==Element&gt;<br>
&gt;&gt;  associatedtype SubSequence: any&lt;Sequence where Sequence.Element == Element&gt;<br>
&gt;&gt;  …<br>
&gt;&gt; }<br>
&gt;<br>
&gt; That&#39;s not really the same thing. Any&lt;IteratorProtocol&gt; is an existential, not a protocol. It&#39;s basically an automatically-generated version of our current `AnyIterator&lt;T&gt;` type (though with some additional flexibility). It can&#39;t appear on the right side of a `:`, any more than AnyIterator could.<br>
<br>
</span>After this proposal you should be able to use these existentials anywhere you can place a constraint, so it would work.  You can do this with the protocol composition operator today and the future existential is just an extension of that capability.<br>
<span><br>
&gt;<br>
&gt; What *would* work is allowing `where` clauses on associated types:<br>
&gt;<br>
&gt;&gt; protocol Sequence {<br>
&gt;&gt;  associatedtype Element<br>
&gt;&gt;  associatedtype Iterator: IteratorProtocol where Iterator.Element==Element<br>
&gt;&gt;  associatedtype SubSequence: Sequence where SubSequence.Element == Element<br>
&gt;&gt;  …<br>
&gt;&gt; }<br>
&gt;<br>
&gt; I believe this is part of the generics manifesto.<br>
&gt;<br>
&gt; --<br>
&gt; Brent Royal-Gordon<br>
&gt; Architechies<br>
&gt;<br>
</span></div></div><span class=""><div><div>&gt; _______________________________________________<br>
&gt; swift-evolution mailing list<br>
&gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></span></blockquote></div><br></div><span class="">
_______________________________________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></span></div></blockquote></div><br></div></blockquote></div><br></div>