Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-4979] SubSequence is not a Sequence, can't be zipped? #47556

Closed
pcantrell opened this issue May 23, 2017 · 3 comments
Closed

[SR-4979] SubSequence is not a Sequence, can't be zipped? #47556

pcantrell opened this issue May 23, 2017 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@pcantrell
Copy link

Previous ID SR-4979
Radar None
Original Reporter @pcantrell
Type Bug
Status Resolved
Resolution Done
Environment

Tested with:

• Swift 3
• a recent Swift 4 snapshot

…in Xcode 8.

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 308b88d2afc5e35dcf218fc68935c491

relates to:

  • SR-1445 Recursive protocol constraints

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

@rintaro
Copy link
Mannequin

rintaro mannequin commented May 24, 2017

This is FIXME thing.
https://github.com/apple/swift/blob/master/stdlib/public/core/Sequence.swift#L333-L346

For now, you can workaround this with extra where clause:

extension Sequence where SubSequence : Sequence, SubSequence.Iterator.Element == Iterator.Element {
    var adjacentPairs: AnySequence<(Iterator.Element, Iterator.Element)> {
        return AnySequence(zip(self, dropFirst()))
    }
}

@pcantrell
Copy link
Author

Ah, right, I see that this is the primary motivation for SE-0157.

Thanks for the workaround, Rintaro!

@slavapestov
Copy link
Member

This has been fixed on master by @DougGregor.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants