[Completing Generics] Arbitrary requirements in protocols


(Jacob Bandes-Storch) #1

Doug wrote this in the Completing Generics manifesto, under "Minor
extensions":

**Arbitrary requirements in protocols*

Currently, a new protocol can inherit from other protocols, introduce new

associated types, and add new conformance constraints to associated types
(by redeclaring an associated type from an inherited protocol). However,
one cannot express more general constraints. Building on the example from
“Recursive protocol constraints”, we really want the element type of a
Sequence’s SubSequence to be the same as the element type of the Sequence,
e.g.,

    protocol Sequence {

        associatedtype Iterator : IteratorProtocol
        …
        associatedtype SubSequence : Sequence where
SubSequence.Iterator.Element == Iterator.Element
    }

+1.

To make it into Swift 3, would this feature require a proposal of its own?
How feasible would it be to implement on top of the current system?

Jacob


(Dave Abrahams) #2

Doug wrote this in the Completing Generics manifesto, under "Minor extensions":

    *Arbitrary requirements in protocols

    Currently, a new protocol can inherit from other protocols, introduce new
    associated types, and add new conformance constraints to associated types
    (by redeclaring an associated type from an inherited protocol). However, one
    cannot express more general constraints. Building on the example from
    “Recursive protocol constraints”, we really want the element type of a
    Sequence’s SubSequence to be the same as the element type of the Sequence,
    e.g.,

    protocol Sequence {
    associatedtype Iterator : IteratorProtocol
    …
    associatedtype SubSequence : Sequence where SubSequence.Iterator.Element ==
    Iterator.Element
    }

+1.

To make it into Swift 3, would this feature require a proposal of its
own?

It could be part of another proposal, but it should be in a proposal.
Whether or not it can still make Swift 3, I am unsure.

How feasible would it be to implement on top of the current system?

I can't answer that, but if you want to work on this I'd suggest
starting with the implementation. In this case, the proposal is the
easy part.

···

on Mon Apr 11 2016, Jacob Bandes-Storch <swift-evolution@swift.org> wrote:

--
Dave


(Douglas Gregor) #3

Doug wrote this in the Completing Generics manifesto, under "Minor extensions":

*Arbitrary requirements in protocols

Currently, a new protocol can inherit from other protocols, introduce new associated types, and add new conformance constraints to associated types (by redeclaring an associated type from an inherited protocol). However, one cannot express more general constraints. Building on the example from “Recursive protocol constraints”, we really want the element type of a Sequence’s SubSequence to be the same as the element type of the Sequence, e.g.,

    protocol Sequence {
        associatedtype Iterator : IteratorProtocol
        …
        associatedtype SubSequence : Sequence where SubSequence.Iterator.Element == Iterator.Element
    }

+1.

To make it into Swift 3, would this feature require a proposal of its own?

Yes. Also, be wary that the syntax above potentially conflicts with the syntax discussed as "moving the where clauses”:

  http://thread.gmane.org/gmane.comp.lang.swift.evolution/13886/focus=14058

How feasible would it be to implement on top of the current system?

Definitely! The archetype builder would need to learn to check these extra where clauses, and one would need to be sure that the constraint solver is picking them up as well.

  - Doug

···

On Apr 11, 2016, at 1:01 AM, Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org> wrote:


#4

Hi Jacob,

I really like the idea of constraining associated types. However I think there could be a much more general solution to this problem by introducing "Partially constrained protocols".

I've already created a (almost finished) proposal for another thread which hasn't continued. It attacks the same issue as "Arbitrary requirements in protocols": https://github.com/Qbyte248/swift-evolution/blob/master/proposals/0000-Partially%20constrained%20protocols%20and%20generic%20types.md

Unfortunately I have (almost) no time to discuss this in depth or completely finish the proposal. So feel free to copy my proposal add yourself as coauthor and start a new thread. Or just take it as an inspiration :slight_smile:

Kind regards
- Maximilian

PS: Looking forward to a new generics model :slight_smile:

···

Am 11.04.2016 um 10:01 schrieb Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org>:

Doug wrote this in the Completing Generics manifesto, under "Minor extensions":

*Arbitrary requirements in protocols

Currently, a new protocol can inherit from other protocols, introduce new associated types, and add new conformance constraints to associated types (by redeclaring an associated type from an inherited protocol). However, one cannot express more general constraints. Building on the example from “Recursive protocol constraints”, we really want the element type of a Sequence’s SubSequence to be the same as the element type of the Sequence, e.g.,

    protocol Sequence {
        associatedtype Iterator : IteratorProtocol
        …
        associatedtype SubSequence : Sequence where SubSequence.Iterator.Element == Iterator.Element
    }

+1.

To make it into Swift 3, would this feature require a proposal of its own? How feasible would it be to implement on top of the current system?

Jacob
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jacob Bandes-Storch) #5

> Doug wrote this in the Completing Generics manifesto, under "Minor
extensions":
>
> *Arbitrary requirements in protocols
>
> Currently, a new protocol can inherit from other protocols,
introduce new
> associated types, and add new conformance constraints to associated
types
> (by redeclaring an associated type from an inherited protocol).
However, one
> cannot express more general constraints. Building on the example from
> “Recursive protocol constraints”, we really want the element type of
a
> Sequence’s SubSequence to be the same as the element type of the
Sequence,
> e.g.,
>
> protocol Sequence {
> associatedtype Iterator : IteratorProtocol
> …
> associatedtype SubSequence : Sequence where
SubSequence.Iterator.Element ==
> Iterator.Element
> }
>
> +1.
>
> To make it into Swift 3, would this feature require a proposal of its
> own?

It could be part of another proposal, but it should be in a proposal.
Whether or not it can still make Swift 3, I am unsure.

> How feasible would it be to implement on top of the current system?

I can't answer that, but if you want to work on this I'd suggest
starting with the implementation. In this case, the proposal is the
easy part.

I was mostly wondering if anyone inside the team has already thought about
how to do this, or perhaps even started working on it. If it's in the
pipeline, it might affect our discussion about "allValues" for enums (which
I'm going to send an email about soon).

···

On Mon, Apr 11, 2016 at 11:56 AM, Dave Abrahams via swift-evolution < swift-evolution@swift.org> wrote:

on Mon Apr 11 2016, Jacob Bandes-Storch <swift-evolution@swift.org> wrote:

--
Dave

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Dave Abrahams) #6

By the way, having this would enable us to massively simplify the
standard library, and potentially lots of user-written generic code,
too. So I'm very excited that someone's interested!

···

on Tue Apr 12 2016, Douglas Gregor <swift-evolution@swift.org> wrote:

    On Apr 11, 2016, at 1:01 AM, Jacob Bandes-Storch via swift-evolution > <swift-evolution@swift.org> wrote:

    Doug wrote this in the Completing Generics manifesto, under "Minor
    extensions":

        *Arbitrary requirements in protocols

        Currently, a new protocol can inherit from other protocols, introduce
        new associated types, and add new conformance constraints to associated
        types (by redeclaring an associated type from an inherited protocol).
        However, one cannot express more general constraints. Building on the
        example from “Recursive protocol constraints”, we really want the
        element type of a Sequence’s SubSequence to be the same as the element
        type of the Sequence, e.g.,

        protocol Sequence {
        associatedtype Iterator : IteratorProtocol
        …
        associatedtype SubSequence : Sequence where SubSequence.Iterator.Element
        == Iterator.Element
        }

    +1.

    To make it into Swift 3, would this feature require a proposal of its own?

Yes. Also, be wary that the syntax above potentially conflicts with the syntax
discussed as "moving the where clauses”:

http://thread.gmane.org/gmane.comp.lang.swift.evolution/13886/focus=14058

    How feasible would it be to implement on top of the current system?

Definitely! The archetype builder would need to learn to check these extra where
clauses, and one would need to be sure that the constraint solver is picking
them up as well.

--
Dave


(David Hart) #7

Hi Doug,

I've read the discussion about moving the where clause to the right of declarations (which I full-heartedly approve) but I don't see how it would have any impact on the syntax of associated types requirements.

David

···

On 12 Apr 2016, at 19:07, Douglas Gregor via swift-evolution <swift-evolution@swift.org> wrote:

On Apr 11, 2016, at 1:01 AM, Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org> wrote:

Doug wrote this in the Completing Generics manifesto, under "Minor extensions":

*Arbitrary requirements in protocols

Currently, a new protocol can inherit from other protocols, introduce new associated types, and add new conformance constraints to associated types (by redeclaring an associated type from an inherited protocol). However, one cannot express more general constraints. Building on the example from “Recursive protocol constraints”, we really want the element type of a Sequence’s SubSequence to be the same as the element type of the Sequence, e.g.,

    protocol Sequence {
        associatedtype Iterator : IteratorProtocol
        …
        associatedtype SubSequence : Sequence where SubSequence.Iterator.Element == Iterator.Element
    }

+1.

To make it into Swift 3, would this feature require a proposal of its own?

Yes. Also, be wary that the syntax above potentially conflicts with the syntax discussed as "moving the where clauses”:

  http://thread.gmane.org/gmane.comp.lang.swift.evolution/13886/focus=14058

How feasible would it be to implement on top of the current system?

Definitely! The archetype builder would need to learn to check these extra where clauses, and one would need to be sure that the constraint solver is picking them up as well.

  - Doug

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Dave Abrahams) #8

AFAIK, nobody has started it yet.

···

on Mon Apr 11 2016, Jacob Bandes-Storch <jtbandes-AT-gmail.com> wrote:

On Mon, Apr 11, 2016 at 11:56 AM, Dave Abrahams via swift-evolution > <swift-evolution@swift.org> wrote:

    on Mon Apr 11 2016, Jacob Bandes-Storch <swift-evolution@swift.org> wrote:

    > Doug wrote this in the Completing Generics manifesto, under "Minor
    extensions":
    >
    > *Arbitrary requirements in protocols
    >
    > Currently, a new protocol can inherit from other protocols, introduce new
    > associated types, and add new conformance constraints to associated types
    > (by redeclaring an associated type from an inherited protocol). However,
    one
    > cannot express more general constraints. Building on the example from
    > “Recursive protocol constraints”, we really want the element type of a
    > Sequence’s SubSequence to be the same as the element type of the Sequence,
    > e.g.,
    >
    > protocol Sequence {
    > associatedtype Iterator : IteratorProtocol
    > …
    > associatedtype SubSequence : Sequence where SubSequence.Iterator.Element
    ==
    > Iterator.Element
    > }
    >
    > +1.
    >
    > To make it into Swift 3, would this feature require a proposal of its
    > own?

    It could be part of another proposal, but it should be in a proposal.
    Whether or not it can still make Swift 3, I am unsure.

    > How feasible would it be to implement on top of the current system?

    I can't answer that, but if you want to work on this I'd suggest
    starting with the implementation. In this case, the proposal is the
    easy part.

I was mostly wondering if anyone inside the team has already thought about how
to do this, or perhaps even started working on it. If it's in the pipeline, it
might affect our discussion about "allValues" for enums (which I'm going to send
an email about soon).

--
Dave


(Jacob Bandes-Storch) #9

I'm interested, but I'm by no means claiming I'll have enough time to drive
any of the discussion/proposal/implementation. :frowning:

Jacob

···

On Tue, Apr 12, 2016 at 3:07 PM, Dave Abrahams via swift-evolution < swift-evolution@swift.org> wrote:

on Tue Apr 12 2016, Douglas Gregor <swift-evolution@swift.org> wrote:

> On Apr 11, 2016, at 1:01 AM, Jacob Bandes-Storch via swift-evolution > > <swift-evolution@swift.org> wrote:
>
> Doug wrote this in the Completing Generics manifesto, under "Minor
> extensions":
>
> *Arbitrary requirements in protocols
>
> Currently, a new protocol can inherit from other protocols,
introduce
> new associated types, and add new conformance constraints to
associated
> types (by redeclaring an associated type from an inherited
protocol).
> However, one cannot express more general constraints. Building
on the
> example from “Recursive protocol constraints”, we really want the
> element type of a Sequence’s SubSequence to be the same as the
element
> type of the Sequence, e.g.,
>
> protocol Sequence {
> associatedtype Iterator : IteratorProtocol
> …
> associatedtype SubSequence : Sequence where
SubSequence.Iterator.Element
> == Iterator.Element
> }
>
> +1.
>
> To make it into Swift 3, would this feature require a proposal of
its own?
>
> Yes. Also, be wary that the syntax above potentially conflicts with the
syntax
> discussed as "moving the where clauses”:
>
>
http://thread.gmane.org/gmane.comp.lang.swift.evolution/13886/focus=14058
>
> How feasible would it be to implement on top of the current system?
>
> Definitely! The archetype builder would need to learn to check these
extra where
> clauses, and one would need to be sure that the constraint solver is
picking
> them up as well.

By the way, having this would enable us to massively simplify the
standard library, and potentially lots of user-written generic code,
too. So I'm very excited that someone's interested!

--
Dave

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Douglas Gregor) #10

Hi Doug,

I've read the discussion about moving the where clause to the right of declarations (which I full-heartedly approve) but I don't see how it would have any impact on the syntax of associated types requirements.

I think “arbitrary requirements in protocols” doesn’t necessarily require them to be attached to an associated type. For example, Collection might require that the SubSequence type itself be a Collection:

  protocol Collection : Sequence {
    associatedtype SubSequence : Collection
  }

Why did we have to declare the SubSequence associated type again (since it’s already down in SubSequence) just to make it conform to Collection. In fact, why are we even allowed to declare a redundant associated type, which we know is defined in Sequence and will have to be the same? Seems to me that we just want a where clause to add the constraint, e.g.,

  protocol Collection : Sequence {
    where SubSequence : Collection
  }

which of course would conflict with moving where clauses later, e.g.,

  protocol Collection : Sequence {
          func foo() // is the following where clause part of foo() or part of Collection?
    where SubSequence : Collection
  }

  - Doug

···

On Apr 12, 2016, at 11:19 PM, David Hart <david@hartbit.com> wrote:

David

On 12 Apr 2016, at 19:07, Douglas Gregor via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Apr 11, 2016, at 1:01 AM, Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Doug wrote this in the Completing Generics manifesto, under "Minor extensions":

*Arbitrary requirements in protocols

Currently, a new protocol can inherit from other protocols, introduce new associated types, and add new conformance constraints to associated types (by redeclaring an associated type from an inherited protocol). However, one cannot express more general constraints. Building on the example from “Recursive protocol constraints”, we really want the element type of a Sequence’s SubSequence to be the same as the element type of the Sequence, e.g.,

    protocol Sequence {
        associatedtype Iterator : IteratorProtocol
        …
        associatedtype SubSequence : Sequence where SubSequence.Iterator.Element == Iterator.Element
    }

+1.

To make it into Swift 3, would this feature require a proposal of its own?

Yes. Also, be wary that the syntax above potentially conflicts with the syntax discussed as "moving the where clauses”:

  http://thread.gmane.org/gmane.comp.lang.swift.evolution/13886/focus=14058

How feasible would it be to implement on top of the current system?

Definitely! The archetype builder would need to learn to check these extra where clauses, and one would need to be sure that the constraint solver is picking them up as well.

  - Doug

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(David Hart) #11

I wouldn't mind driving the discussion and proposal, because I'd really like to see a more complete generics system. Before I start, can David or Doug, or someone else with a high-level view of the generics system tell me if this is where to start or if there is another feature in the Complete Generics manifesto which is more urgent first?

David

···

On 13 Apr 2016, at 01:46, Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org> wrote:

I'm interested, but I'm by no means claiming I'll have enough time to drive any of the discussion/proposal/implementation. :frowning:

Jacob

On Tue, Apr 12, 2016 at 3:07 PM, Dave Abrahams via swift-evolution <swift-evolution@swift.org> wrote:

on Tue Apr 12 2016, Douglas Gregor <swift-evolution@swift.org> wrote:

> On Apr 11, 2016, at 1:01 AM, Jacob Bandes-Storch via swift-evolution >> > <swift-evolution@swift.org> wrote:
>
> Doug wrote this in the Completing Generics manifesto, under "Minor
> extensions":
>
> *Arbitrary requirements in protocols
>
> Currently, a new protocol can inherit from other protocols, introduce
> new associated types, and add new conformance constraints to associated
> types (by redeclaring an associated type from an inherited protocol).
> However, one cannot express more general constraints. Building on the
> example from “Recursive protocol constraints”, we really want the
> element type of a Sequence’s SubSequence to be the same as the element
> type of the Sequence, e.g.,
>
> protocol Sequence {
> associatedtype Iterator : IteratorProtocol
> …
> associatedtype SubSequence : Sequence where SubSequence.Iterator.Element
> == Iterator.Element
> }
>
> +1.
>
> To make it into Swift 3, would this feature require a proposal of its own?
>
> Yes. Also, be wary that the syntax above potentially conflicts with the syntax
> discussed as "moving the where clauses”:
>
> http://thread.gmane.org/gmane.comp.lang.swift.evolution/13886/focus=14058
>
> How feasible would it be to implement on top of the current system?
>
> Definitely! The archetype builder would need to learn to check these extra where
> clauses, and one would need to be sure that the constraint solver is picking
> them up as well.

By the way, having this would enable us to massively simplify the
standard library, and potentially lots of user-written generic code,
too. So I'm very excited that someone's interested!

--
Dave

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Douglas Gregor) #12

I wouldn't mind driving the discussion and proposal, because I'd really like to see a more complete generics system. Before I start, can David or Doug, or someone else with a high-level view of the generics system tell me if this is where to start or if there is another feature in the Complete Generics manifesto which is more urgent first?

I think this is a fine feature to focus on. It’s useful, fits well in the system, and it’s scope is small enough that it’s achievable.

  - Doug

···

On Apr 12, 2016, at 11:23 PM, David Hart via swift-evolution <swift-evolution@swift.org> wrote:

David

On 13 Apr 2016, at 01:46, Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

I'm interested, but I'm by no means claiming I'll have enough time to drive any of the discussion/proposal/implementation. :frowning:

Jacob

On Tue, Apr 12, 2016 at 3:07 PM, Dave Abrahams via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

on Tue Apr 12 2016, Douglas Gregor <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

> On Apr 11, 2016, at 1:01 AM, Jacob Bandes-Storch via swift-evolution >> > <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
>
> Doug wrote this in the Completing Generics manifesto, under "Minor
> extensions":
>
> *Arbitrary requirements in protocols
>
> Currently, a new protocol can inherit from other protocols, introduce
> new associated types, and add new conformance constraints to associated
> types (by redeclaring an associated type from an inherited protocol).
> However, one cannot express more general constraints. Building on the
> example from “Recursive protocol constraints”, we really want the
> element type of a Sequence’s SubSequence to be the same as the element
> type of the Sequence, e.g.,
>
> protocol Sequence {
> associatedtype Iterator : IteratorProtocol
> …
> associatedtype SubSequence : Sequence where SubSequence.Iterator.Element
> == Iterator.Element
> }
>
> +1.
>
> To make it into Swift 3, would this feature require a proposal of its own?
>
> Yes. Also, be wary that the syntax above potentially conflicts with the syntax
> discussed as "moving the where clauses”:
>
> http://thread.gmane.org/gmane.comp.lang.swift.evolution/13886/focus=14058
>
> How feasible would it be to implement on top of the current system?
>
> Definitely! The archetype builder would need to learn to check these extra where
> clauses, and one would need to be sure that the constraint solver is picking
> them up as well.

By the way, having this would enable us to massively simplify the
standard library, and potentially lots of user-written generic code,
too. So I'm very excited that someone's interested!

--
Dave

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Dave Abrahams) #13

I think Doug is somewhat understating its usefulness :wink:

···

on Wed Apr 13 2016, Douglas Gregor <dgregor-AT-apple.com> wrote:

    On Apr 12, 2016, at 11:23 PM, David Hart via swift-evolution > <swift-evolution@swift.org> wrote:

    I wouldn't mind driving the discussion and proposal, because I'd really like
    to see a more complete generics system. Before I start, can David or Doug,
    or someone else with a high-level view of the generics system tell me if
    this is where to start or if there is another feature in the Complete
    Generics manifesto which is more urgent first?

I think this is a fine feature to focus on. It’s useful, fits well in the
system, and it’s scope is small enough that it’s achievable.

--
Dave