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

Thorsten Seitz tseitz42 at icloud.com
Fri May 27 07:08:44 CDT 2016


> Am 25.05.2016 um 22:16 schrieb Matthew Johnson via swift-evolution <swift-evolution at swift.org>:
> 
> 
>> On May 25, 2016, at 2:22 PM, Brent Royal-Gordon <brent at architechies.com> wrote:
>> 
>>> 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
>> 
>> 	let value: Any<HashableAndComparable> = 123
>> 	
>> 	func sum<C: CollectionOfConforming<Integer>>(numbers: C) -> C.Iterator.Element {
>> 		return numbers.reduce(0, combine: +)
>> 	}
> 
> If we do something specific to generic constraints I would prefer to see something that generalizes to support cases where you want to accept two types that both conform to `Sequence`, `Collection`, etc and both have the same `Element` (or any other constraint that relates associated types from more than one type argument).  It could be similar to what you have, but slightly more generalized.
> 
> 
> func sum
>        <T, C1, C2
>        where C1: Sequence, C2: Sequence, C1.Iterator.Element == T, C2.Iterator.Element == T>
>        (c1: C1, c2: C2, op: (T, T) -> T) -> [T] {
>        return zip(c1, c2).map(op)
> }
> 
> This would allow me to write something along the lines of:
> 
> func zipMap
>        <S1, S2 where SameElementSequences<S1, S2>>
>        (s1:S1, s2: S2, op: (S1.Element, S2.Element) -> S1.Element) -> [S1.Element] {
>        return zip(s1, s2).map(op)
> }
> 
> So what syntax do we use to define something like `SameElementSequences<S1, S2 >`?  It is effectively a predicate that returns true if all of the constraints it defines are satisfied.  It could be similar to what you have above, but we would need to allow for more than one “constrainee”.
> 
> Maybe it would look something like this:
> 
> constraint SameElementSequences S1, S2 = 
>        S1: Sequence, S2: Sequence
>        where S1.Element == S2.Element

That’s a good example! Now this is beginning to make sense (or now I’m starting to understand it :-)


Side note: I would prefer zipMap being defined more generally which unfortunately renders this obsolete as an example for abstracted constraints as none are needed anymore:

func zipMap<S1: Sequence, S2: Sequence, Result>
	(s1: S1, s2: S2, op: (S1.Element, S2.Element) -> Result) -> [Result] {
	return zip(s1, s2).map(op)
}


But we might want to create a generic subtraction function like follows which could make use of the constraint you defined further down:

func subtract<S1, S2, E where SequencesOf<E, S1, S2>, E: Equatable>
	(s1: S1, s2: S2) -> [E] {
	// answer array containing all elements of s1 which are not in s2
}

Note that `E` is not a concrete type argument here. I’m not sure whether it makes sense to distinguish between `Type` and `Constrainee` as these kinds are simply a result of supplying either a concrete type or a type parameter:

constraint SequencesOf<Element, S1, S2> =
       S1: Sequence, S2: Sequence
       where S1.Element == Element, S2.Element == Element

Regardless whether Element, S1 or S2 are concrete types or type parameters, the constraint can simply be checked whether it holds.


Maybe we could change the syntax a little bit:

constraint SequencesOf<Element, S1, S2> where S1: Sequence, S2: Sequence, S1.Element == Element, S2.Element == Element

which might alternatively be written as:

constraint SequencesOf<Element, S1: Sequence, S2: Sequence> where S1.Element == Element, S2.Element == Element


This would distinguish constraints better from typealiases. Otherwise a simple constraint would look like a typealias:

constraint SequenceOf<Element, S> = S where S: Sequence, Element == S.Element

vs.

constraint SequenceOf<Element, S> where S: Sequence, Element == S.Element

-Thorsten


> 
> If we want to allow concrete types and higher order constraints to be passed we would have to specify “kinds” for the arguments to the constraints.  We might say `Constrainee` for an argument that is getting constrained, `Type` for a concrete type argument and `Constraint` for a higher order constraint that applies to a single type and something like function type syntax for multi-argument higher order constraints: `(Type, Constrainee, Constraint) -> Constraint` or something like that.
> 
> This would let us do something like:
> 
> constraint SequencesOf Element: Type, S1: Constrainee, S2: Constrainee =
>        S1: Sequence, S2: Sequence
>        where S1.Element == Element, S2.Element == Element
> 
> Used like this: 
> 
> func intZipMap
>        <S1, S2 where SequencesOf<Int, S1, S2>>
>        (s1:S1, s2: S2, op: (Int) -> Int) -> [Int] {
>        return zip(s1, s2).map(op)
> }
> 
> We could allow shorthand for simple constraints where the “kinds” are omitted and assumed to be `Constrainee`.
> 
> Ideally we would be able to overload constraints so we could use the name “SequencesOf” for a constraint that accepts three sequences, etc.
> 
> I think I prefer `constraint` rather than `constraintalias` but am open to arguments for both of them.
> 
> I like that you are allowed a `constraintalias` to be used as an existential.  That would still work for single argument constraints in the generalized form I am suggesting.
> 
> I’m just spitballing on syntax and keyword names here to try and communicate the capability that I think we should strive for.  I’m interested in hearing everyone’s thoughts on this…
> 
> -Matthew
> 
>> 
>> -- 
>> Brent Royal-Gordon
>> Architechies
>> 
> 
> _______________________________________________
> 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/20160527/e7bb3ec3/attachment.html>


More information about the swift-evolution mailing list