[swift-evolution] Enhanced existential types proposal discussion

Austin Zheng austinzheng at gmail.com
Thu May 26 15:44:57 CDT 2016


(inline)

On Thu, May 26, 2016 at 12:22 PM, David Hart <david at hartbit.com> wrote:

> Hi Austin,
>
> I never had te occasion to say thanks for the work you have put in this
> proposal, so thanks! I’m really looking forward to be able to have some
> form of it accepted and implemented in Swift.
>

Thank you! I just hope a proposal like this one ends up being good enough
that it means less work for the core team, not more...


>
> Here are a few comments:
>
> 1) Why would Any<> and Any<NSView> be illegal? What error messages would
> they generate? Why not make them simply synonymous to Any, and NSView,
> the same way protocol<> currently behaves?
>

"Any<>" being illegal is a syntactic battle that is being fought over in a
different thread; I'm not personally invested one way or another. (We might
not even adopt "Any" syntax specifically; Joe Groff has ideas for a
different syntax that doesn't use the brackets.)

"Any<NSView>" is an existential, and "NSView" isn't. Existentials'
metatypes are different from the metatypes of concrete types, and the ways
they can be used with generics is different as well. My opinion is that
Any<...> signifies an existential, and allowing the use of "Any<SomeClass>"
as a concrete type would just confuse people even more.


>
> 2) You say that "An Any<...> existential can have zero or
> one where clauses, *following the list of requirements.*” This seems to
> be contradicted in two places:
>

> In the example under *Nested Any<...>*, which should probably be edited
> to:
>
> // NOT ALLOWED// This is impossible to fufill. A collection's elements cannot be both strings// and integers at the same time.let a : Any<Collection, Any<Collection where Collection.Element == Int> where Collection.Element == String>
>
>
>
Yes, my wording should have been clearer. What I meant is that you can't
have more than one where clause in the *current* Any<>, but you can nest
another Any<> that has its own where clause. I'll edit the copy.


> In the *where clause* section, you say:
>
> Associated types used within the where clause must belong to the
> protocols in the current or previous requirements.
>
> This sounds a bit bizarre because where clauses always appear at the end,
> so there is no such “current” requirements and all requirements are
> “previous”.
>

You are right; I'll fix that. The weird verbiage is a remnant of an earlier
draft where each protocol could have its own where clause.


>
> I still have to take the time to finish digesting the end of the proposal.
>

Happy to clarify or answer any questions you might have.


>
> David.
>
> On 26 May 2016, at 07:53, Austin Zheng via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> 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.
>
> 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.
>
> 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.
>
> 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.
>
> 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.
>
> As always, the link is here, and feedback would be greatly appreciated:
> https://github.com/austinzheng/swift-evolution/blob/az-existentials/proposals/XXXX-enhanced-existentials.md
>
> Best,
> Austin
>
> On Tue, May 24, 2016 at 5:09 AM, Matthew Johnson via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>>
>>
>> Sent from my iPad
>>
>> On May 23, 2016, at 9:52 PM, Brent Royal-Gordon via swift-evolution <
>> swift-evolution at swift.org> wrote:
>>
>> >> 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.
>> >>
>> >> protocol Sequence {
>> >>  associatedtype Element
>> >>  associatedtype Iterator: any<IteratorProtocol where
>> IteratorProtocol.Element==Element>
>> >>  associatedtype SubSequence: any<Sequence where Sequence.Element ==
>> Element>
>> >>  …
>> >> }
>> >
>> > 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.
>>
>> 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.
>>
>> >
>> > What *would* work is allowing `where` clauses on associated types:
>> >
>> >> protocol Sequence {
>> >>  associatedtype Element
>> >>  associatedtype Iterator: IteratorProtocol where
>> Iterator.Element==Element
>> >>  associatedtype SubSequence: Sequence where SubSequence.Element ==
>> Element
>> >>  …
>> >> }
>> >
>> > I believe this is part of the generics manifesto.
>> >
>> > --
>> > Brent Royal-Gordon
>> > Architechies
>> >
>> > _______________________________________________
>> > swift-evolution mailing list
>> > swift-evolution at swift.org
>> > https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160526/ee057f65/attachment.html>


More information about the swift-evolution mailing list