Skip to content

[SR-413] AnySequence does not implement all methods on SequenceType #43030

Closed
@swift-ci

Description

@swift-ci
Previous ID SR-413
Radar None
Original Reporter bnut (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee None
Priority Medium

md5: f1f0d117a5204e887ab4187dd39016b6

relates to:

  • SR-4499 Iterating over elements from existential collections is super slow

Issue Description:

TL;DR

At the moment (0...1000000000).suffix(2) is really efficient.
However, AnySequence(0...1000000000).suffix(2) is not.

Long version

AnySequence implements underestimateCount and generate, however it does not implement any of the other methods on SequenceType.

This means that if you've provided custom implementations of these methods it still uses the implementation from SequenceType's protocol extension.

For example:

struct MySequence: SequenceType {
    private static let array = [1,2,3]
    func generate() -> AnyGenerator<Int> {
        return anyGenerator(MySequence.array.generate())
    }
    func suffix(maxLength: Int) -> AnySequence<Int> {
        return AnySequence(MySequence.array.suffix(maxLength).map{$0 + 1})
    }
}

let a = MySequence()
let b = AnySequence(a)

print("\(Array(a.suffix(2))) == \(Array(b.suffix(2)))?")

Outputs:

[3, 4]==[2, 3]?

but it should output:

[3, 4]==[3, 4]?

This example is probably not something you would want to do, but it is representative of the problem. Other cases this may be an issue are when implementing infinite sequences, implementing suffix efficiently on a bidirectional indexed collection type, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.standard libraryArea: Standard library umbrella

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions