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-7797] Incorrect results using subscript in lazy collection #50336

Open
swift-ci opened this issue May 29, 2018 · 5 comments
Open

[SR-7797] Incorrect results using subscript in lazy collection #50336

swift-ci opened this issue May 29, 2018 · 5 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 29, 2018

Previous ID SR-7797
Radar None
Original Reporter TT_Kilew (JIRA User)
Type Bug
Environment

Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.37.1)

Xcode: Version 9.3 (9E145)

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

md5: 1db7d23c2a2c01e9614b9f873e4d2a63

Issue Description:

The current swift version has a bug when lazy and non-lazy collections behaves differently when accessed via subscript

Lazy Collection

print("=== LAZY ===")

let lazyCollection = ["1", "2", "3", "4"]
    .lazy
    .filter {
        print("...lazy filtering \($0)")
        return ($0 == "3" || $0 == "4")
    }
print(lazyCollection[0])

Output:

=== LAZY ===
1

Non-Lazy Collection

print("=== NON-LAZY ===")
let nonLazyCollection = ["1", "2", "3", "4"]
    .filter {
        print("...filtering \($0)")
        return ($0 == "3" || $0 == "4")
    }

print(nonLazyCollection[0])

Output:

=== NON-LAZY ===
...filtering 1
...filtering 2
...filtering 3
...filtering 4
3

So lazy collection behaves totally different from the non-lazy collection, which doesn't seem like a correct behaviour

@belkadan
Copy link
Contributor

Lazy collections use the same indexes as their underlying collections, so (as I understand it) it's assumed that if you do direct access using an index, you want an element from the underlying collection. If you want the first element, you can use .first.

@lorentey, does that sound correct?

@lorentey
Copy link
Member

It does! The lazy filter collection reuses the index type of the original collection, although typically only a subset of the original indices are valid in it. In this case, 0 is an invalid index in lazyCollection; unfortunately the index is just passed through to the underlying collection, with no validation. (Validating the index would involve calling the filter closure, which would be unexpected.)

This wrinkle doesn't occur when using the collection APIs to generate indices in lazyCollection; for example, print(nonLazyCollection[nonLazyCollection.startIndex]) prints 3.

One way to prevent these issues would be to wrap the base collection's indices into a new LazyCollection.Index structure, which could only be generated by LazyCollection's own APIs. @moiseev, do you think we should consider doing that?

@belkadan
Copy link
Contributor

I think the main reason they use the underlying collection's index is so that you can use things like index(where:) on the lazy collection and get something useful. It'd also be pretty source-breaking to change.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jun 7, 2018

Comment by Petro Korienev (JIRA)

While it sounds reasonable, it's really confusing for the developers because:

The possible solution will be to run complete lazy calculation chain once:

  • startIndex/endIndex/formIndex is accessed

  • subscript is accessed

and then cache the result into some `_calculated` property discarding `_base` collection?

If changes to LazyCollection are off the roadmap for Swift 4.2 minors / Swift 5, probably adding proper documentation for this case can be considered to be taken into implementation?

Thanks in advance

@moiseev
Copy link
Mannequin

moiseev mannequin commented Jun 14, 2018

/cc @natecook1000

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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

3 participants