<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 8, 2016, at 1:33 PM, Dave Abrahams <<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class=""><br class="">on Tue Jun 07 2016, Matthew Johnson <<a href="http://matthew-at-anandabits.com" class="">matthew-AT-anandabits.com</a>> wrote:<br class=""><br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Jun 7, 2016, at 9:15 PM, Dave Abrahams <<a href="mailto:dabrahams@apple.com" class="">dabrahams@apple.com</a>> wrote:<br class=""><br class=""><br class="">on Tue Jun 07 2016, Matthew Johnson <<a href="http://matthew-at-anandabits.com" class="">matthew-AT-anandabits.com</a> <<a href="http://matthew-at-anandabits.com/" class="">http://matthew-at-anandabits.com/</a>>> wrote:<br class=""><br class=""></blockquote><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Jun 7, 2016, at 4:13 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""><br class="">on Tue Jun 07 2016, Matthew Johnson <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><br class=""></blockquote><br class=""><blockquote type="cite" class=""><blockquote type="cite" class=""><blockquote type="cite" class="">, but haven't realized<br class="">that if you step around the type relationships encoded in Self<br class="">requirements and associated types you end up with types that appear to<br class="">interoperate but in fact trap at runtime unless used in exactly the<br class="">right way.<br class=""></blockquote><br class="">Trap at runtime? How so? Generalized existentials should still be<br class="">type-safe. <br class=""></blockquote><br class="">There are two choices when you erase static type relationships:<br class=""><br class="">1. Acheive type-safety by trapping at runtime<br class=""><br class="">FloatingPoint(3.0 as Float) + FloatingPoint(3.0 as Double) // trap<br class=""><br class="">2. Don't expose protocol requirements that involve these relationships,<br class=""> which would prevent the code above from compiling and prevent<br class=""> FloatingPoint from conforming to itself.<br class=""><br class=""><blockquote type="cite" class="">Or are you talking about the hypothetical types / behaviors people<br class="">think they want when they don’t fully understand what is happening...<br class=""></blockquote><br class="">I don't know what you mean here. I think generalized existentials will<br class="">be nice to have, but I think most people will want them to do something<br class="">they can't possibly do.<br class=""></blockquote><br class="">Exactly. What I meant is that people think they want that expression<br class="">to compile because they don’t understand that the only thing it can do<br class="">is trap. I said “hypothetical” because producing a compile time error<br class="">rather than a runtime trap is the only sane thing to do. Your comment<br class="">surprised me because I can’t imagine we would move forward in Swift<br class="">with the approach of trapping.<br class=""></blockquote><br class="">I would very much like to be able to create instances of “Collection<br class="">where Element == Int” so we can throw away the wrappers in the stdlib.<br class="">That will require some type mismatches to be caught at runtime via<br class="">trapping.<br class=""></blockquote><br class="">For invalid index because the existential accepts a type erased index?<br class=""></blockquote><br class="">Exactly.<br class=""><br class=""><blockquote type="cite" class="">How do you decide where to draw the line here? It feels like a very<br class="">slippery slope for a language where safety is a stated priority to<br class="">start adopting a strategy of runtime trapping for something as<br class="">fundamental as how you expose members on an existential.<br class=""></blockquote><br class="">If you don't do this, the alternative is that “Collection where Element<br class="">== Int” does not conform to Collection. </div></div></blockquote><div><br class=""></div><div>This isn’t directly related to having self or associated type requirements. It is true of all existentials. If that changes for simple existentials and generalized existentials expose all members (as in the latest draft of the proposal) maybe it will be possible for all existentials to conform to their protocol. </div><br class=""><blockquote type="cite" class=""><div class=""><div class="">That's weird and not very<br class="">useful. You could expose all the methods that were on protocol<br class="">extensions of Collection on this existential, unless they used<br class="">associated types other than the element type. But you couldn't pass the<br class="">existential to a generic function like<br class=""><br class=""> func scrambled<C: Collection>(_ c: C) -> [C.Element]<br class=""><br class=""><blockquote type="cite" class="">IMO you should *have* to introduce unsafe behavior like that manually.<br class=""></blockquote><br class=""> Collection where Element == Int & Index == *<br class=""><br class="">?<br class=""></div></div></blockquote><div><br class=""></div><div>I didn’t mean directly through the type of the existential.</div><div><br class=""></div><div>One obvious mechanism for introducing unsafe behavior is to write manual type erasure wrappers like we do today. </div><div><br class=""></div><div>Another possibility would be to allow extending the existential type (not the protocol). This would allow you to write overloads on the Collection existential that takes some kind of type erased index if that is what you want and either trap if you receive an invalid index or better (IMO) return an `Element?`. I’m not sure how extensions on existentials might be implemented, but this is an example of the kind of operation you might want available on it that you wouldn’t want available on all Collection types.</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class=""><blockquote type="cite" class="">Collection indices are already something that isn’t fully statically<br class="">safe so I understand why you might want to allow this. <br class=""></blockquote><br class="">By the same measure, so are Ints :-)<br class=""><br class="">The fact that a type's methods have preconditions does *not* make it<br class="">“statically unsafe.”<br class=""></div></div></blockquote><div><br class=""></div><div>That depends on what you mean by safe. Sure, those methods aren’t going corrupt memory, but they *are* going to explicitly and intentionally crash for some inputs. That doesn’t qualify as “<b class="">fully</b> safe” IMO.</div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class=""><blockquote type="cite" class="">But I don’t think having the language's existentials do this<br class="">automatically is the right approach. Maybe there is another approach<br class="">that could be used in targeted use cases where the less safe behavior<br class="">makes sense and is carefully designed.<br class=""></blockquote><br class="">Whether it makes sense or not really depends on the use-cases. There's<br class="">little point in generalizing existentials if the result isn't very useful.<br class=""></div></div></blockquote><div><br class=""></div><div>Usefulness depends on your perspective. I have run into several scenarios where they would be very useful without needing to be prone to crashes when used incorrectly. One obvious basic use case is storing things in a heterogenous collection where you bind .</div><br class=""><blockquote type="cite" class=""><div class=""><div class="">The way to find out is to take a look at the examples we currently have<br class="">of protocols with associated types or Self requirements and consider<br class="">what you'd be able to do with their existentials if type relationships<br class="">couldn't be erased. <br class=""><br class="">We have known use-cases, currently emulated in the standard library, for<br class="">existentials with erased type relationships. *If* these represent the<br class="">predominant use cases for something like generalized existentials, it<br class="">seems to me that the language feature should support that. Note: I have<br class="">not seen anyone build an emulation of the other kind of generalized<br class="">existential. My theory: there's a good reason for that :-).<br class=""></div></div></blockquote><div><br class=""></div><div>AFAIK (and I could be wrong) the only rules in the language that require the compiler to synthesize a trap except using a nil IUO, `!` on a nil Optional, and an invalid `as` cast . These are all syntactically explicit unsafe / dangerous operations. All other traps are in the standard library (array index, overflow, etc). Most important about all of these cases is that they have received direct human consideration. </div><div><br class=""></div><div>Introducing a language (not library) mechanism that exposes members on generalized existentials in a way that relies on runtime traps for type safety feels to me like a pretty dramatic turn agains the stated priority of safety. It will mean you must understand exactly what is going on and be extremely careful to use generalized existentials without causing crashes. This will either make Swift code much more crashy or will scare people away from using generalized existentials (and maybe both). Neither of those outcomes is good. </div><div><br class=""></div><div>Collection indices are a somewhat special case as there is already a strong precondition that people are familiar with because it would be too costly to performance and arguably too annoying to deal with an Optional result in every array lookup. IMO that is why the library is able to get away with it in the current type erased AnyCollection. But this is not a good model for exposing any members on an existential that do not already have a strong precondition that causes a trap when violated.</div><div><br class=""></div><div>I think a big reason why you maybe haven’t seen a lot of examples of people writing type erased “existentials" is because it is a huge pain in the neck to write this stuff manually today. People may be designing around the need for them. I haven’t seen a huge sampling of type erased “existentials" other people are writing but I haven’t written any that introduce a trap like this. The only traps are in the “abstract" base class whose methods will never be called (and wouldn’t even be implemented if they could be marked abstract).</div><div><br class=""></div><div>What specific things do you think we need to be able to do that rely on the compiler synthesizing a trap in the way it exposes the members of the existential? </div><div><br class=""></div><div>Here are a few examples from Austin’s proposal that safely use existential collections. I don’t understand why you think this approach is insufficient. Maybe you could supply a concrete example of a use case that can’t be written with the mechanism in Austin’s proposal.</div><div><br class=""></div><div><a href="https://github.com/austinzheng/swift-evolution/blob/az-existentials/proposals/XXXX-enhanced-existentials.md#associated-types-and-member-exposure" class="">https://github.com/austinzheng/swift-evolution/blob/az-existentials/proposals/XXXX-enhanced-existentials.md#associated-types-and-member-exposure</a></div><div><br class=""></div><div><div class="highlight highlight-source-swift" style="box-sizing: border-box; margin-bottom: 16px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, 'Segoe UI', Arial, freesans, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol'; font-size: 16px; background-color: rgb(255, 255, 255);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', 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;" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> a <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Any</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);"><</span>Collection<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">></span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// A variable whose type is the Index associated type of the underlying</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// concrete type of 'a'.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> theIndex <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> a<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>Index <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// A variable whose type is the Element associated type of the underlying</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// concrete type of 'a'.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> theElement <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> a<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>Element <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span></pre><div class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);"><br class=""></span></div><div class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);"><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', 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; color: rgb(51, 51, 51);" class=""><span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Given a mutable collection, swap its first and last items.</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Not a generic function. </span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">swapFirstAndLast</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">inout</span> <span class="pl-smi" style="box-sizing: border-box;">collection</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Any</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);"><</span>BidirectionalMutableCollection<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">></span>) {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// firstIndex and lastIndex both have type "collection.Index"</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">guard</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> firstIndex <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> collection<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">startIndex</span>,
lastIndex <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> collection<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">endIndex</span>?<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">predecessor</span>(collection) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">where</span> lastIndex <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">!=</span> firstIndex <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">else</span> {
<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">print</span>(<span class="pl-s" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>Nothing to do<span class="pl-pds" style="box-sizing: border-box;">"</span></span>)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">return</span>
}
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// oldFirstItem has type "collection.Element"</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> oldFirstItem <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> collection[firstIndex]
collection[firstIndex] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> collection[lastIndex]
collection[lastIndex] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> oldFirstItem
}</pre><div class=""><br class=""></div><div class=""><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', 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; color: rgb(51, 51, 51);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> a <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Any</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);"><</span>BidirectionalMutableCollection <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">where</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>Element <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">==</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">></span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">...</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> input <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-s" style="box-sizing: border-box; color: rgb(24, 54, 145);"><span class="pl-pds" style="box-sizing: border-box;">"</span>West Meoley<span class="pl-pds" style="box-sizing: border-box;">"</span></span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// Not actually necessary, since the compiler knows "a.Element" is String.</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// A fully constrained anonymous associated type is synonymous with the concrete</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// type it's forced to take on, and the two are interchangeable.</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// However, 'as' casting is still available if desired.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> anonymousInput <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> input <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as</span> a<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>Element
a[a<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">startIndex</span>] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> anonymousInput
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// as mentioned, this also works:</span>
a[a<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">startIndex</span>] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> input</pre><div class=""><br class=""></div></div><div class=""><pre style="box-sizing: border-box; font-family: Consolas, 'Liberation Mono', 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; color: rgb(51, 51, 51);" class=""><span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// If the collection allows it, set the first element in the collection to a given string.</span>
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">setFirstElementIn</span>(<span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">inout</span> <span class="pl-smi" style="box-sizing: border-box;">collection</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Any</span><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);"><</span>Collection<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">></span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">toString</span> <span class="pl-smi" style="box-sizing: border-box;">string</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>) {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">if</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> element <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> string <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">as?</span> collection<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span>Element {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);">// At this point, 'element' is of type "collection.Element"</span>
collection[collection<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">.</span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">startIndex</span>] <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> element
}
}</pre><div class=""><br class=""></div></div></span></div></div></div><br class=""><blockquote type="cite" class=""><div class=""><div class=""><br class="">-- <br class="">Dave<br class=""></div></div></blockquote></div><br class=""></body></html>