Uploaded image for project: 'Swift'
  1. Swift
  2. SR-4979

SubSequence is not a Sequence, can't be zipped?

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Standard Library
    • Labels:
      None
    • Environment:

      Tested with:

      • Swift 3
      • a recent Swift 4 snapshot

      …in Xcode 8.

      Description

      This code works:

      extension Array {
        var adjacentPairs: AnySequence<(Iterator.Element, Iterator.Element)> {
          return AnySequence(
            zip(self, dropFirst()))
        }
      }
      
      let a = ["car", "bon", "fire", "fly", "way", "farer"]
      for (w0, w1) in a.adjacentPairs {
        print(w0 + w1)
      }
      

      But change that extension from Array to Sequence, and it doesn’t compile:

      extension Sequence {  // ← only change
        var adjacentPairs: AnySequence<(Iterator.Element, Iterator.Element)> {
          return AnySequence(
            zip(self, dropFirst()))  // ← compile error here
        }
      }
      

      The error is:

      argument type 'Self.SubSequence' does not conform to expected type 'Sequence'

      This seems like a problem in the Sequence protocol: we should be able to assume that every SubSequence is a Sequence … right? Am I missing something here?

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                pcantrell Paul Cantrell
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: