> On May 27, 2016, at 7:08 AM, Thorsten Seitz <[email protected]> wrote:
> 
>> 
>> Am 25.05.2016 um 22:16 schrieb Matthew Johnson via swift-evolution 
>> <[email protected] <mailto:[email protected]>>:
>> 
>> 
>>> On May 25, 2016, at 2:22 PM, Brent Royal-Gordon <[email protected] 
>>> <mailto:[email protected]>> 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 :-)

Great!

> 
> 
> 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:

Yeah, I wasn’t trying to provide the best / most general design for `zipMap`.  
I chose a signature that would demonstrate how you might want to abstract over 
constraints that apply to more than one constrainee.  It was the first thing 
that came to mind.  :-)

> 
> 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.

The reason it matters is that you *must* pass something that can be constrained 
to a `Constrainee` argument.  You *cannot* pass a concrete type.  This could be 
inferred, but I think that would fall into the same category as inference 
across a function signature, which Swift is specifically avoiding.  Thus the 
“kind” specification.

> 
> 
> 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

I avoided angle brackets on the arguments because I think it’s more readable 
without them.  But there is a good case to be made that angle brackets should 
be used for compile-time parameters in Swift.  This syntax works for me.  

One thing that just occurred to me when looking at your example:

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

I wonder if we would ever need the ability to pass a constrained but concrete 
type to a constraint abstraction.  I can’t quickly think of any examples where 
we would.  If we don’t need the ability to do this, we could easily infer that 
`S1` and `S2` are of kind `Constrainee` because they are constrained in the 
signature.  We could also say that the default kind is `Type`.  This would 
minimize the need to state and think explicitly about kinds.

-Matthew

> 
> -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
>> [email protected] <mailto:[email protected]>
>> https://lists.swift.org/mailman/listinfo/swift-evolution 
>> <https://lists.swift.org/mailman/listinfo/swift-evolution>
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to