<commit_before># Constraining `AnySequence.init`

* Proposal: [SE-0014](https://github.com/apple/swift-evolution/blob/master/proposals/0014-constrained-AnySequence.md)
* Author(s): [<NAME>](https://github.com/moiseev)
* Status: **Under review** (December 18--21, 2015)
* Review Manager: [<NAME>](https://github.com/DougGregor)

## Introduction

In order to allow `AnySequence` delegate calls to the underlying sequence,
its initializer should have extra constraints.

## Motivation

At the moment `AnyCollection` does not delegate calls to `SequenceType` protocol
methods to the underlying base sequence, which results in dynamic downcasts in
places where this behavior is needed (see default implementations of
`SequenceType.dropFirst` or `SequenceType.prefix`). Besides, and this is even
more important, customized implementations of `SequenceType` methods would be
ignored without delegation.

## Proposed solution

See the implementation in [this PR](https://github.com/apple/swift/pull/220).

In order for this kind of delegation to become possible, `_SequenceBox` needs to
be able to 'wrap' not only the base sequence but also it's associated
`SubSequence`. So instead of being declared like this:

~~~~Swift
internal class _SequenceBox<S : SequenceType>
    : _AnySequenceBox<S.Generator.Element> { ... }
~~~~

it would become this:

~~~~Swift
internal class _SequenceBox<
  S : SequenceType
  where
    S.SubSequence : SequenceType,
    S.SubSequence.Generator.Element == S.Generator.Element,
    S.SubSequence.SubSequence == S.SubSequence
> : _AnySequenceBox<S.Generator.Element> { ... }
~~~~

Which, in it's turn, will lead to `AnySequence.init` getting a new set of
constraints as follows.

Before the change:

~~~~Swift
public struct AnySequence<Element> : SequenceType {
  public init<<commit_msg>[editorial] Correct overuse of apostrophe<commit_after># Constraining `AnySequence.init`

* Proposal: [SE-0014](https://github.com/apple/swift-evolution/blob/master/proposals/0014-constrained-AnySequence.md)
* Author(s): [<NAME>](https://github.com/moiseev)
* Status: **Under review** (December 18--21, 2015)
* Review Manager: [<NAME>](https://github.com/DougGregor)

## Introduction

In order to allow `AnySequence` delegate calls to the underlying sequence,
its initializer should have extra constraints.

## Motivation

At the moment `AnyCollection` does not delegate calls to `SequenceType` protocol
methods to the underlying base sequence, which results in dynamic downcasts in
places where this behavior is needed (see default implementations of
`SequenceType.dropFirst` or `SequenceType.prefix`). Besides, and this is even
more important, customized implementations of `SequenceType` methods would be
ignored without delegation.

## Proposed solution

See the implementation in [this PR](https://github.com/apple/swift/pull/220).

In order for this kind of delegation to become possible, `_SequenceBox` needs to
be able to 'wrap' not only the base sequence but also its associated
`SubSequence`. So instead of being declared like this:

~~~~Swift
internal class _SequenceBox<S : SequenceType>
    : _AnySequenceBox<S.Generator.Element> { ... }
~~~~

it would become this:

~~~~Swift
internal class _SequenceBox<
  S : SequenceType
  where
    S.SubSequence : SequenceType,
    S.SubSequence.Generator.Element == S.Generator.Element,
    S.SubSequence.SubSequence == S.SubSequence
> : _AnySequenceBox<S.Generator.Element> { ... }
~~~~

Which, in its turn, will lead to `AnySequence.init` getting a new set of
constraints as follows.

Before the change:

~~~~Swift
public struct AnySequence<Element> : SequenceType {
  public init<
{'fraction_non_alphanumeric': 0.10696653867251783, 'fraction_numerical': 0.010422380691168404, 'mean_word_length': 5.028099173553719, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6070306', 'n_tokens_mistral': 1081, 'n_tokens_neox': 993, 'n_words': 363}