<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 Mar 2, 2016, at 7:57 PM, Developer via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="content-type" content="text/html; charset=utf-8" class=""><div dir="auto" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><blockquote type="cite" class=""><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><b class=""><font class="">Existentials</font></b><br class=""></span></font></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></font></div><div class=""><i class="" style="background-color: rgba(255, 255, 255, 0);"><font class="">Opening existentials</font></i></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></font></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">Generalized existentials as described above will still have trouble with protocol requirements that involve Self or associated types in function parameters. For example, let’s try to use Equatable as an existential:</span></font></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></font></div><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);">protocol Equatable {</font></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);">&nbsp; func ==(lhs: Self, rhs: Self) -&gt; Bool</font></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);">&nbsp; func !=(lhs: Self, rhs: Self) -&gt; Bool</font></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);">}</font></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);"><br class=""></font></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);">let e1: Equatable = …</font></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);">let e2: Equatable = …</font></div><div class=""><font class="" style="background-color: rgba(255, 255, 255, 0);">if e1 == e2 { … }&nbsp;<i class="">//&nbsp;<b class="">error</b>:</i>&nbsp;e1 and e2 don’t necessarily have the same dynamic type</font></div></blockquote><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></font></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">One explicit way to allow such operations in a type-safe manner is to introduce an “open existential” operation of some sort, which extracts and gives a name to the dynamic type stored inside an existential. For example:</span></font></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></font></div><div class=""><font class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>&nbsp;</span></font></div><div class=""><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><font class="" style="background-color: rgba(255, 255, 255, 0);">if let storedInE1 = e1 openas T { &nbsp; &nbsp; // T is a the type of storedInE1, a copy of the value stored in e1</font></blockquote><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><font class="" style="background-color: rgba(255, 255, 255, 0);">&nbsp; if let storedInE2 = e2 as? T { &nbsp; &nbsp; &nbsp;// is e2 also a T?</font></blockquote><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><font class="" style="background-color: rgba(255, 255, 255, 0);">&nbsp; &nbsp; if storedInE1 == storedInE2 {&nbsp;… } // okay: storedInT1 and storedInE2 are both of type T, which we know is Equatable</font></blockquote><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><font class="" style="background-color: rgba(255, 255, 255, 0);">&nbsp; }</font></blockquote><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><font class="" style="background-color: rgba(255, 255, 255, 0);">}</font></blockquote></div></blockquote><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class=""><br class=""></span></div><div class=""><span style="background-color: rgba(255, 255, 255, 0);" class="">Isn't "open existential" code for "casting ∃ to ∀"? Dispatch on the underlying type is brittle and anti-modular.</span></div></div></div></div></div></blockquote><br class=""></div><div>I'm not sure exactly what you're referring to, but our existential type representation captures the type and protocol conformance metadata that was statically available when the existential value was constructed, so I don't think there's a modularity problem. This operation would rebind that metadata to a type conforming to the protocol in the scope where the type binding is introduced.</div><div><br class=""></div><div>-Joe</div></body></html>