Why doesn't Collection's SubSequence conform to Collection?


(Charles Srstka) #1

The following comment accompanies the declaration of the SubSequence associated type in the Collection protocol:

/// A sequence that represents a contiguous subrange of the collection's
/// elements.
///
/// This associated type appears as a requirement in the `Sequence`
/// protocol, but it is restated here with stricter constraints. In a
/// collection, the subsequence should also conform to `Collection`.
associatedtype SubSequence : IndexableBase, Sequence = Slice<Self>

The comments clearly state that the subsequence should conform to Collection, however, it is not declared as such. Why is this?

Charles


(Max Moiseev) #2

Because it would be recursive. which is not supported, I believe.

protocol Foo {
    associatedtype Bar : Foo
}

Playground execution failed: error: MyPlayground.playground:2:20: error: type may not reference itself as a requirement
    associatedtype Bar : Foo
                   ^

···

On Feb 13, 2017, at 1:42 PM, Charles Srstka via swift-evolution <swift-evolution@swift.org> wrote:

The following comment accompanies the declaration of the SubSequence associated type in the Collection protocol:

/// A sequence that represents a contiguous subrange of the collection's
/// elements.
///
/// This associated type appears as a requirement in the `Sequence`
/// protocol, but it is restated here with stricter constraints. In a
/// collection, the subsequence should also conform to `Collection`.
associatedtype SubSequence : IndexableBase, Sequence = Slice<Self>

The comments clearly state that the subsequence should conform to Collection, however, it is not declared as such. Why is this?

Charles

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


(Huon Wilson) #3

The lines just after the current declaration show the desired version, which is waiting on two generics features that are in the pipeline:

  // FIXME(ABI)#98 (Recursive Protocol Constraints):
  // FIXME(ABI)#99 (Associated Types with where clauses):
  // associatedtype SubSequence : Collection
  // where
  // Iterator.Element == SubSequence.Iterator.Element,
  // SubSequence.Index == Index,
  // SubSequence.Indices == Indices,
  // SubSequence.SubSequence == SubSequence
  //
  // (<rdar://problem/20715009 <rdar://problem/20715009>> Implement recursive protocol
  // constraints)
  //
  // These constraints allow processing collections in generic code by
  // repeatedly slicing them in a loop.

Huon

···

On Feb 13, 2017, at 13:42, Charles Srstka via swift-evolution <swift-evolution@swift.org> wrote:

The following comment accompanies the declaration of the SubSequence associated type in the Collection protocol:

/// A sequence that represents a contiguous subrange of the collection's
/// elements.
///
/// This associated type appears as a requirement in the `Sequence`
/// protocol, but it is restated here with stricter constraints. In a
/// collection, the subsequence should also conform to `Collection`.
associatedtype SubSequence : IndexableBase, Sequence = Slice<Self>

The comments clearly state that the subsequence should conform to Collection, however, it is not declared as such. Why is this?

Charles

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


(Douglas Gregor) #4

The compiler doesn’t support “recursive” constraints. We’re working on it

  https://gist.github.com/DougGregor/e7c4e7bb4465d6f5fa2b59be72dbdba6

  - Doug

···

On Feb 13, 2017, at 1:42 PM, Charles Srstka via swift-evolution <swift-evolution@swift.org> wrote:

The following comment accompanies the declaration of the SubSequence associated type in the Collection protocol:

/// A sequence that represents a contiguous subrange of the collection's
/// elements.
///
/// This associated type appears as a requirement in the `Sequence`
/// protocol, but it is restated here with stricter constraints. In a
/// collection, the subsequence should also conform to `Collection`.
associatedtype SubSequence : IndexableBase, Sequence = Slice<Self>

The comments clearly state that the subsequence should conform to Collection, however, it is not declared as such. Why is this?


(Davide Mendolia) #5

What's the status of Recursive protocol constraints? it's a feature that
will help the design of a library I'm working on.
How can I help ?

···

On Tue, Feb 14, 2017 at 1:32 AM Douglas Gregor via swift-evolution < swift-evolution@swift.org> wrote:

On Feb 13, 2017, at 1:42 PM, Charles Srstka via swift-evolution < > swift-evolution@swift.org> wrote:

The following comment accompanies the declaration of the SubSequence
associated type in the Collection protocol:

/// A sequence that represents a contiguous subrange of the collection's
/// elements.
///
/// This associated type appears as a requirement in the `Sequence`
/// protocol, but it is restated here with stricter constraints. In a
/// collection, the subsequence should also conform to `Collection`.
associatedtype SubSequence : IndexableBase, Sequence = Slice<Self>

The comments clearly state that the subsequence should conform to
Collection, however, it is not declared as such. Why is this?

The compiler doesn’t support “recursive” constraints. We’re working on it

https://gist.github.com/DougGregor/e7c4e7bb4465d6f5fa2b59be72dbdba6

- Doug

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


(Douglas Gregor) #6

What's the status of Recursive protocol constraints? it's a feature that will help the design of a library I'm working on.

It’s a surprisingly intricate feature that’s required some major refactoring in the compiler (for the better).

How can I help ?

The review of this proposal (SE-0157) is active now, so feel free to weigh in if you haven’t already.

  - Doug

···

On Feb 24, 2017, at 7:49 AM, Davide Mendolia <davide@gokarumi.com> wrote:

On Tue, Feb 14, 2017 at 1:32 AM Douglas Gregor via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On Feb 13, 2017, at 1:42 PM, Charles Srstka via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

The following comment accompanies the declaration of the SubSequence associated type in the Collection protocol:

/// A sequence that represents a contiguous subrange of the collection's
/// elements.
///
/// This associated type appears as a requirement in the `Sequence`
/// protocol, but it is restated here with stricter constraints. In a
/// collection, the subsequence should also conform to `Collection`.
associatedtype SubSequence : IndexableBase, Sequence = Slice<Self>

The comments clearly state that the subsequence should conform to Collection, however, it is not declared as such. Why is this?

The compiler doesn’t support “recursive” constraints. We’re working on it

  https://gist.github.com/DougGregor/e7c4e7bb4465d6f5fa2b59be72dbdba6

  - Doug

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