<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 8, 2016 at 3:22 PM, Dave Abrahams <span dir="ltr">&lt;<a href="mailto:dabrahams@apple.com" target="_blank">dabrahams@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span class=""><br>
on Wed Jun 08 2016, Austin Zheng &lt;austinzheng-AT-gmail.com&gt; wrote:<br>
<br>
&gt; FWIW my opinion is that existentials either shouldn&#39;t be allowed to stand<br>
&gt; in for generic type parameters, or Dave&#39;s option #1 if they are.<br>
<br>
</span>Don&#39;t you mean #2?  Otherwise I&#39;m confused.  #1 is the one that<br>
prohibits more usages.<br></blockquote><div><br></div><div>I&#39;m just being cautious until a better solution comes along.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<span class=""><br>
&gt; The implied promise of a generic type parameter T right now is that T<br>
&gt; always stands for the same concrete type (modulo things like passing<br>
&gt; in a subclass where a class would do), and likewise for all of T&#39;s<br>
&gt; associated types (T.Foo is always the same type everywhere in the<br>
&gt; context where T is valid). This is what makes using anything with<br>
&gt; &#39;self&#39; requirements in a generic context sound. Allowing existentials<br>
&gt; to satisfy T would violate that constraint.<br>
<br>
</span>Not if you consider Any&lt;Collection where Element == Int&gt; to be a<br>
concrete type.  Concrete w.r.t. to a generic parameter means something<br>
different from Concrete w.r.t. subtyping.<br></blockquote><div><br></div><div>You can consider Any&lt;Collection where .Element == Int&gt; to be a concrete type, but then you have the unprecedented situation where the associated types associated with a concrete type aren&#39;t necessarily the same for all instances (is this true for any type that can satisfy a generic type parameter today?).</div><div><br></div><div>(For the sake of this argument, Array isn&#39;t a concrete type, but Array&lt;T&gt; or Array&lt;Int&gt; is. You can&#39;t use &#39;Array&#39; anywhere in Swift today, so I think my assertion is fair.)</div><div><br></div><div>My understanding is that fixing the generic type parameter T by specializing a function/type also fixes all the associated types associated with T. This &#39;contract&#39; would have to be weakened to allow existential types to satisfy generic type parameters in any non-trivial way.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<span class=""><br>
&gt; Relaxing these semantics would make it too easy to write code that<br>
&gt; traps at runtime &quot;without the user having to reach&quot; (to paraphrase<br>
&gt; Jordan from the &quot;Swift philosophy&quot; thread). Anyone who really really<br>
&gt; wants to write code that is &#39;compile-time unsound&#39; in this way should<br>
&gt; have to explicitly type erase using concrete wrappers.<br>
<br>
</span>I&#39;d really like to see the use-cases, to make sure that these restricted<br>
existentials will be useful enough to be worth implementing.<br></blockquote><div><br></div><div>This is enormously important.</div><div><br></div><div>First of all, (of course you know) there is a semantic difference between:</div><div><br></div><div>protocol Pet : class { }; class Cat : Pet { }; class Dog : Pet { }</div><div><br></div><div>class AnimalShelter&lt;T : Pet&gt; { var pet: T }</div><div><br></div><div>and </div><div><br></div><div>class AnimalShelter { var pet: Pet }</div><div><br></div><div>This is something you can express for simple existentials today (the code above should run, with minor modifications), but not for existentials containing associated types or self requirements.</div><div><br></div><div>I think a big part of what people want to do is to declare variables, pass args to functions, and get return values from functions that abstract over something like Collection. They want to do this without having to make their code generic, and without forcing their code to be homogenous at runtime (e.g. an instance of the dynamic AnimalShelter can be populated with a cat and later a dog, but the generic one can only ever contain either; extend this to Collections of Ints or whatnot).</div><div><br></div><div>The big problem is that existentials can&#39;t guarantee that they satisfy the contract generic functions and types are expecting, as we&#39;ve been discussing.</div><div><br></div><div>To be honest, I think requiring existentials to be opened should be a prerequisite to using them generically. This would define away the impedance mismatch at the expense of making certain things marginally harder to do. (An alternate way of thinking about it is that it would make the potential for a runtime error explicit, and localize it):</div><div><br></div><div>func doSomething&lt;T : Collection where T.Element == Int&gt;(x: C, y: C) { ... }</div><div><br></div><div>let a : Any&lt;Collection where .Element == Int&gt;</div><div>let b : Any&lt;Collection where .Element == Int&gt;</div><div><br></div><div>// Prohibit this...</div><div>// doSomething(a, b)</div><div><br></div><div>// Allow this:</div><div>if let a = a openas? T, b = b as? T {</div><div>  // We&#39;ve recovered the &#39;strong guarantee&#39; that doSomething expects for T</div><div>  doSomething(a, b)</div><div>} else {</div><div>  // Here&#39;s our trap</div><div>}</div><div><br></div><div>The biggest problem is that this sort of solution would prohibit existentials from being used in generic contexts where the existentials only have to be &quot;similar enough&quot;, not identical, for things to work out. Given how fuzzy &quot;similar enough&quot; has proven to be, maybe that&#39;s not a terrible tradeoff.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div class=""><div class="h5"><br>
&gt;<br>
&gt;<br>
&gt; Best,<br>
&gt; Austin<br>
&gt;<br>
&gt; On Wed, Jun 8, 2016 at 2:37 PM, Austin Zheng &lt;<a href="mailto:austinzheng@gmail.com">austinzheng@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt;&gt; We might be talking past each other. I think Matthew is talking about<br>
&gt;&gt; using an existential outside the context of generic functions. For example,<br>
&gt;&gt; something like this should be trap-proof (as long as &#39;x&#39; is immutable,<br>
&gt;&gt; which it is in this example):<br>
&gt;&gt;<br>
&gt;&gt; func copySequenceIntoArray(x: Any&lt;Sequence where .Iterator.Element ==<br>
&gt;&gt; Int&gt;) -&gt; [Int] {<br>
&gt;&gt; var buffer : [Int] = []<br>
&gt;&gt;         // Stupid implementation to make a point<br>
&gt;&gt; var iterator : x.Iterator = x.makeIterator()<br>
&gt;&gt; while true {<br>
&gt;&gt; let nextItem : Int? = iterator.next()<br>
&gt;&gt; if let nextItem = nextItem {<br>
&gt;&gt; buffer.append(nextItem)<br>
&gt;&gt; } else {<br>
&gt;&gt; return buffer<br>
&gt;&gt; }<br>
&gt;&gt; }<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; Even this would never trap as well:<br>
&gt;&gt;<br>
&gt;&gt; func copySequenceIntoArray&lt;T&gt;(x: Any&lt;Sequence where .Iterator.Element ==<br>
&gt;&gt; T&gt;) -&gt; [T] {<br>
&gt;&gt; var buffer : [T] = []<br>
&gt;&gt; for item in x {<br>
&gt;&gt; buffer.append(item)<br>
&gt;&gt; }<br>
&gt;&gt; return buffer<br>
&gt;&gt; }<br>
&gt;&gt;<br>
&gt;&gt; Where we run into difficulty is something like this (forgive my abuse of<br>
&gt;&gt; the collections API; I don&#39;t remember all the new indexing APIs off the top<br>
&gt;&gt; of my head):<br>
&gt;&gt;<br>
&gt;&gt; func doSomething&lt;T : Collection where T.Element == Int&gt;(x: T, y: T) {<br>
&gt;&gt; // Get indexes out of x and use them to index into y<br>
&gt;&gt; var idx = x.startIndex<br>
&gt;&gt; while (idx != x.endIndex || idx != y.endIndex) {<br>
&gt;&gt; print(x[idx])<br>
&gt;&gt; print(y[idx])<br>
&gt;&gt; idx = x.nextIndex(idx)<br>
&gt;&gt; }<br>
&gt;&gt; }<br>
&gt;&gt; let someSeq : Any&lt;Collection where .Element == Int&gt; = // ...<br>
&gt;&gt; let anotherSeq : Any&lt;Collection where .Element == Int&gt; = // ...<br>
&gt;&gt; // Trouble!<br>
&gt;&gt; // someSeq and anotherSeq are the same existential type<br>
&gt;&gt; // But the concrete index types within each of the existential variables<br>
&gt;&gt; may be different<br>
&gt;&gt; doSomething(someSeq, anotherSeq)<br>
&gt;&gt;<br>
&gt;&gt; It&#39;s this situation (using an existential type to fulfill a generic type<br>
&gt;&gt; parameter constrained to the same requirements that comprise that<br>
&gt;&gt; existential) that requires either of the two options that Dave presented,<br>
&gt;&gt; due to our lack of compile-time type information about the fulfilling<br>
&gt;&gt; type&#39;s associated types.<br>
&gt;&gt;<br>
&gt;&gt; Best,<br>
&gt;&gt; Austin<br>
&gt;&gt;<br>
&gt;&gt; On Wed, Jun 8, 2016 at 2:33 PM, Matthew Johnson via swift-evolution &lt;<br>
&gt;&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Sent from my iPad<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt; On Jun 8, 2016, at 3:16 PM, Dave Abrahams via swift-evolution &lt;<br>
&gt;&gt;&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;&gt; on Wed Jun 08 2016, Thorsten Seitz &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; Ah, thanks, I forgot!  I still consider this a bug, though (will have<br>
&gt;&gt;&gt; &gt;&gt; to read up again what the reasons are for that behavior).<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; Yes, but in the case of the issue we&#39;re discussing, the choices are:<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; 1. Omit from the existential&#39;s API any protocol requirements that depend<br>
&gt;&gt;&gt; &gt;   on Self or associated types, in which case it *can&#39;t* conform to<br>
&gt;&gt;&gt; &gt;   itself because it doesn&#39;t fulfill the requirements.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; They don&#39;t need to be omitted.  They are exposed in different ways<br>
&gt;&gt;&gt; depending on how the existential is constrained.  Austin&#39;s proposal was<br>
&gt;&gt;&gt; originally written to omit some members but it was modified based on<br>
&gt;&gt;&gt; feedback from Doug Gregor IIRC (Austin, is that right?).  Now it contains<br>
&gt;&gt;&gt; examples showing how these members are made available in a safe way.   Some<br>
&gt;&gt;&gt; members may still not be usable because you can&#39;t form an argument but IIRC<br>
&gt;&gt;&gt; the suggestion was that they be exposed anyway for consistency.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; 2. Erase type relationships and trap at runtime when they don&#39;t line up.<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; Matthew has been arguing against #2, but you can&#39;t “fix the bug” without<br>
&gt;&gt;&gt; &gt; it.<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; -Thorsten<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; Am 08.06.2016 um 21:43 schrieb Austin Zheng &lt;<a href="mailto:austinzheng@gmail.com">austinzheng@gmail.com</a>&gt;:<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; It&#39;s not possible, even with Swift&#39;s current implementation of<br>
&gt;&gt;&gt; &gt;&gt;&gt; existentials. A protocol type P isn&#39;t considered to conform to<br>
&gt;&gt;&gt; &gt;&gt;&gt; itself, thus the following is rejected:<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; let a : MyProtocol = // ...<br>
&gt;&gt;&gt; &gt;&gt;&gt; func myFunc&lt;T : MyProtocol&gt;(x: T) {<br>
&gt;&gt;&gt; &gt;&gt;&gt;  // ....<br>
&gt;&gt;&gt; &gt;&gt;&gt; }<br>
&gt;&gt;&gt; &gt;&gt;&gt; myFunc(a) // &quot;Cannot invoke &#39;myFunc&#39; with an argument list of type<br>
&gt;&gt;&gt; MyProtocol&quot;<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; Changing how this works is probably worth a proposal by itself.<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; Austin<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; On Wed, Jun 8, 2016 at 12:34 PM, Thorsten Seitz via swift-evolution<br>
&gt;&gt;&gt; &gt;&gt;&gt; &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt; &gt;&gt;&gt; &lt;mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; wrote:<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; Am 08.06.2016 um 20:33 schrieb Dave Abrahams via swift-evolution<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; &lt;mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;&gt;:<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; on Tue Jun 07 2016, Matthew Johnson &lt;matthew-AT-anandabits.com&gt;<br>
&gt;&gt;&gt; wrote:<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; On Jun 7, 2016, at 9:15 PM, Dave Abrahams<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; &lt;<a href="mailto:dabrahams@apple.com">dabrahams@apple.com</a><br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; &lt;mailto:<a href="mailto:dabrahams@apple.com">dabrahams@apple.com</a>&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; wrote:<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; on Tue Jun 07 2016, Matthew Johnson &lt;matthew-AT-anandabits.com<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; &lt;<a href="http://matthew-at-anandabits.com/" rel="noreferrer" target="_blank">http://matthew-at-anandabits.com/</a><br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; &lt;<a href="http://matthew-at-anandabits.com/" rel="noreferrer" target="_blank">http://matthew-at-anandabits.com/</a>&gt;&gt;&gt; wrote:<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; On Jun 7, 2016, at 4:13 PM, Dave Abrahams via swift-evolution<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; &lt;mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; wrote:<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; on Tue Jun 07 2016, Matthew Johnson<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; &lt;mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; wrote:<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; , but haven&#39;t realized<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; that if you step around the type relationships encoded in Self<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; requirements and associated types you end up with types that<br>
&gt;&gt;&gt; appear to<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; interoperate but in fact trap at runtime unless used in<br>
&gt;&gt;&gt; exactly the<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; right way.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Trap at runtime?  How so?  Generalized existentials should<br>
&gt;&gt;&gt; still be<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; type-safe.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; There are two choices when you erase static type relationships:<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; 1. Acheive type-safety by trapping at runtime<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; FloatingPoint(3.0 as Float) + FloatingPoint(3.0 as Double) //<br>
&gt;&gt;&gt; trap<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; 2. Don&#39;t expose protocol requirements that involve these<br>
&gt;&gt;&gt; relationships,<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; which would prevent the code above from compiling and prevent<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; FloatingPoint from conforming to itself.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; Or are you talking about the hypothetical types / behaviors<br>
&gt;&gt;&gt; people<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; think they want when they don’t fully understand what is<br>
&gt;&gt;&gt; happening...<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; I don&#39;t know what you mean here.  I think generalized<br>
&gt;&gt;&gt; existentials will<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; be nice to have, but I think most people will want them to do<br>
&gt;&gt;&gt; something<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; they can&#39;t possibly do.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; Exactly.  What I meant is that people think they want that<br>
&gt;&gt;&gt; expression<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; to compile because they don’t understand that the only thing it<br>
&gt;&gt;&gt; can do<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; is trap.  I said “hypothetical” because producing a compile time<br>
&gt;&gt;&gt; error<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; rather than a runtime trap is the only sane thing to do.  Your<br>
&gt;&gt;&gt; comment<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; surprised me because I can’t imagine we would move forward in<br>
&gt;&gt;&gt; Swift<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;&gt; with the approach of trapping.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; I would very much like to be able to create instances of<br>
&gt;&gt;&gt; “Collection<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; where Element == Int” so we can throw away the wrappers in the<br>
&gt;&gt;&gt; stdlib.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; That will require some type mismatches to be caught at runtime via<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;&gt; trapping.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; For invalid index because the existential accepts a type erased<br>
&gt;&gt;&gt; index?<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; Exactly.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; How do you decide where to draw the line here?  It feels like a very<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; slippery slope for a language where safety is a stated priority to<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; start adopting a strategy of runtime trapping for something as<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; fundamental as how you expose members on an existential.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; If you don&#39;t do this, the alternative is that “Collection where<br>
&gt;&gt;&gt; Element<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; == Int” does not conform to Collection.  That&#39;s weird and not very<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; useful.  You could expose all the methods that were on protocol<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; extensions of Collection on this existential, unless they used<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; associated types other than the element type.  But you couldn&#39;t pass<br>
&gt;&gt;&gt; the<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; existential to a generic function like<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;  func scrambled&lt;C: Collection&gt;(_ c: C) -&gt; [C.Element]<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; I don’t understand. Why couldn’t an existential be passed to that<br>
&gt;&gt;&gt; function?<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; -Thorsten<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; IMO you should *have* to introduce unsafe behavior like that<br>
&gt;&gt;&gt; manually.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; Collection where Element == Int &amp; Index == *<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; ?<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; Collection indices are already something that isn’t fully statically<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; safe so I understand why you might want to allow this.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; By the same measure, so are Ints :-)<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; The fact that a type&#39;s methods have preconditions does *not* make it<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; “statically unsafe.”<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; But I don’t think having the language&#39;s existentials do this<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; automatically is the right approach.  Maybe there is another<br>
&gt;&gt;&gt; approach<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; that could be used in targeted use cases where the less safe<br>
&gt;&gt;&gt; behavior<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;&gt; makes sense and is carefully designed.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; Whether it makes sense or not really depends on the use-cases.<br>
&gt;&gt;&gt; There&#39;s<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; little point in generalizing existentials if the result isn&#39;t very<br>
&gt;&gt;&gt; useful.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; The way to find out is to take a look at the examples we currently<br>
&gt;&gt;&gt; have<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; of protocols with associated types or Self requirements and consider<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; what you&#39;d be able to do with their existentials if type<br>
&gt;&gt;&gt; relationships<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; couldn&#39;t be erased.<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; We have known use-cases, currently emulated in the standard library,<br>
&gt;&gt;&gt; for<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; existentials with erased type relationships.  *If* these represent<br>
&gt;&gt;&gt; the<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; predominant use cases for something like generalized existentials, it<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; seems to me that the language feature should support that.  Note: I<br>
&gt;&gt;&gt; have<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; not seen anyone build an emulation of the other kind of generalized<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; existential.  My theory: there&#39;s a good reason for that :-).<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; --<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; Dave<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; swift-evolution mailing list<br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; &lt;mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;&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>
&gt;&gt;&gt; &gt;&gt;&gt;&gt; &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; &gt;&gt;&gt; swift-evolution mailing list<br>
&gt;&gt;&gt; &gt;&gt;&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt; &gt;&gt;&gt; &lt;mailto:<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;<br>
&gt;&gt;&gt; &gt;&gt;&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>
&gt;&gt;&gt; &gt;&gt;&gt; &lt;<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a>&gt;<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; &gt;&gt; swift-evolution mailing list<br>
&gt;&gt;&gt; &gt;&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt; &gt;&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>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; --<br>
&gt;&gt;&gt; &gt; Dave<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; _______________________________________________<br>
&gt;&gt;&gt; &gt; swift-evolution mailing list<br>
&gt;&gt;&gt; &gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt; &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>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; swift-evolution mailing list<br>
&gt;&gt;&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&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>
&gt;&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
<br>
</div></div><span class=""><font color="#888888">--<br>
Dave<br>
</font></span></blockquote></div><br></div></div>