[swift-evolution] [swift-evolution-announce] [Review] SE-0089: Replace protocol<P1, P2> syntax with Any<P1, P2>

Matthew Johnson matthew at anandabits.com
Fri May 27 10:03:54 CDT 2016


> On May 27, 2016, at 6:16 AM, Thorsten Seitz <tseitz42 at icloud.com> wrote:
> 
> 
>> Am 25.05.2016 um 21:22 schrieb Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org>:
>> 
>>> But if we are going to remove the ability to use typealiases bound to `Any` in constraints we need to introduce an alternative mechanism for factoring out constraints (hopefully a superior mechanism that can abstract over constraints that relate generic parameters to each other).
>> 
>> I could certainly imagine having, for instance, a `constraintalias` keyword:
>> 
>> 	constraintalias HashableAndComparable = Hashable, Comparable
>> 	constraintalias CollectionOfConforming<ElementConstraint> = Collection where .Element: ElementConstraint
> 
> The second one is not really a constraintalias but a regular generic typealias making use of an abstracted constraint, isn’t it?
> 
> If we use `P & Q` instead of `Any<P, Q>` and just `P` instead of `Any<P>` we not only avoid the confusion between `Any<P>` being the same as `P` in case of P not having associated types or self constraints, we also don’t have to distinguish between using existentials as types and constraints, and we don’t have to introduce something like constraintalias, because typealias is sufficient.

Typealias is already sufficient anyway (try it today with a typealias of a protocol<> type).  IIUC Brent just wants to draw a stronger distinction between syntax you use for existential types and syntax you use for constraints.

One thing that is different from what is possible today in both of your examples is that you are passing a higher-order constraint.  That is really cool but it isn’t possible today and is somewhat orthogonal to the syntax used to describe existentials and I imagine it is down the road a ways further than generalized existentials.

> 
> i.e.
> 
> typealias HashableAndComparable = Hashable & Comparable
> 
> typealias CollectionOfConforming<ElementConstraint> = Collection where .Element: ElementConstraint
> 
> let a: HashableAndComparable = 123
> 
> let b: Hashable & Comparable = 123
> 
> func sum<C: CollectionOfConforming<Integer>>(numbers: C) -> C.Iterator.Element {
> 	return numbers.reduce(0, combine: +)
> }
> 
> -Thorsten
> 
> 



More information about the swift-evolution mailing list