<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><span></span></div><div><div><br><br>Sent from my iPad</div><div><br>On May 26, 2016, at 12:52 PM, Thorsten Seitz via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=utf-8"><br class=""><div><blockquote type="cite" class=""><div class="">Am 26.05.2016 um 07:53 schrieb Austin Zheng via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">The inimitable Joe Groff provided me with an outline as to how the design could be improved. I've taken the liberty of rewriting parts of the proposal to account for his advice.<div class=""><br class=""></div><div class="">It turns out the runtime type system is considerably more powerful than I expected. The previous concept in which protocols with associated types' APIs were vended out selectively and using existentials has been discarded.</div><div class=""><br class=""></div><div class="">Instead, all the associated types that belong to an existential are accessible as 'anonymous' 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's value's underlying type's associated type.</div><div class=""><br class=""></div></div></div></blockquote><div><br class=""></div><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);">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>
</pre><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);">// 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=""><br class=""></div><div class="">In Scala this would be a „path dependent type“ which actually depends on the variable `a`.</div><div class="">What would happen in the following case:</div><div class=""><br class=""></div><div class="">func foo<T: Any<Collection>>(a: T, b: T) {</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>// is the type of a.Element equal to the type of b.Element here? (In Scala it would not) </div><div class="">}</div><div class=""><br class=""></div></div></div></blockquote><div><br></div><div>As Austin noted, this is unrelated to existentials. The constraint is identical to 'T: Collection'. Allowing 'Any' in constraints is just a convenient way to factor them out (by using a typealias).</div><div><br></div><div>Because there is only one type parameter there is only one Element. But because a and b are not existential values you can't access the associated type of the instance directly (that is something new in Austin's proposal). You have to use either T or dynamicType.</div><div><br></div><div><span style="background-color: rgba(255, 255, 255, 0);"> func foo<T: Collection>(a: T, b: T) {<br> print(a.dynamicType.Iterator.Element.self == b.dynamicType.Iterator.Element.self)<br> }<br> // prints true<br> foo(a: [1, 2, 3], b: [4, 5, 6])</span></div><div><br></div><div>Austin, this brings to mind the question of how you will handle conflicts if the type defines an instance member with the same name as the asociatedtype. This should be uncommon due to capitalization conventions but is still a possibility.</div><br><blockquote type="cite"><div><div><div class=""><br class=""></div><div class="">-Thorsten</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="">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 class=""><br class=""></div><div class="">A set of conversions has also been defined. At compile-time 'as' can be used to turn values of these anonymous associated types back into existentials based on the constraints defined earlier. 'as?' can also be used for conditional casting of these anonymously-typed values into potential actual types.</div><div class=""><br class=""></div><div class="">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" class="">https://github.com/austinzheng/swift-evolution/blob/az-existentials/proposals/XXXX-enhanced-existentials.md</a></div><div class=""><br class=""></div><div class="">Best,</div><div class="">Austin</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, May 24, 2016 at 5:09 AM, Matthew Johnson via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br class="">
<br class="">
Sent from my iPad<br class="">
<span class=""><br class="">
On May 23, 2016, at 9:52 PM, Brent Royal-Gordon via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class="">
<br class="">
>> 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 class="">
>><br class="">
>> protocol Sequence {<br class="">
>> associatedtype Element<br class="">
>> associatedtype Iterator: any<IteratorProtocol where IteratorProtocol.Element==Element><br class="">
>> associatedtype SubSequence: any<Sequence where Sequence.Element == Element><br class="">
>> …<br class="">
>> }<br class="">
><br class="">
> That's not really the same thing. Any<IteratorProtocol> is an existential, not a protocol. It's basically an automatically-generated version of our current `AnyIterator<T>` type (though with some additional flexibility). It can't appear on the right side of a `:`, any more than AnyIterator could.<br class="">
<br class="">
</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 class="">
<span class="im HOEnZb"><br class="">
><br class="">
> What *would* work is allowing `where` clauses on associated types:<br class="">
><br class="">
>> protocol Sequence {<br class="">
>> associatedtype Element<br class="">
>> associatedtype Iterator: IteratorProtocol where Iterator.Element==Element<br class="">
>> associatedtype SubSequence: Sequence where SubSequence.Element == Element<br class="">
>> …<br class="">
>> }<br class="">
><br class="">
> I believe this is part of the generics manifesto.<br class="">
><br class="">
> --<br class="">
> Brent Royal-Gordon<br class="">
> Architechies<br class="">
><br class="">
</span><div class="HOEnZb"><div class="h5">> _______________________________________________<br class="">
> swift-evolution mailing list<br class="">
> <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
<br class="">
_______________________________________________<br class="">
swift-evolution mailing list<br class="">
<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">
</div></div></blockquote></div><br class=""></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></div></body></html>