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-9923] Misleading subscript in LazySequence of Array #52329

Open
swift-ci opened this issue Feb 14, 2019 · 4 comments
Open

[SR-9923] Misleading subscript in LazySequence of Array #52329

swift-ci opened this issue Feb 14, 2019 · 4 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-9923
Radar None
Original Reporter chungdan (JIRA User)
Type Bug

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler, Standard Library
Labels Bug
Assignee @belkadan
Priority Medium

md5: 97b354c0b29f50e48e0912d32ebeb487

Issue Description:

There are bug in LazySequence.

When I test LazySequence with subscript, I got some unexpected results.

I expected that the result of print(lazyArrayElement[2]) is 20 like print(filterdArrayElement[2]). But it print 8.

Is this Bug? or right? I think this is wrong.

@belkadan
Copy link
Contributor

This is correct behavior: the subscript on a lazy collection uses the same indexes as the original collection, rather than counting from the beginning every time. It would be nice if we could warn about this somehow, though. cc @airspeedswift

@airspeedswift
Copy link
Member

I'm not sure how we could generate a warning (or how you'd suppress the warning if it is what you meant). The code is valid.

The only option I'd see (which is not an option at this point given it'd be source breaking) is to not reuse the index and opaquify the index type with a wrapper, like ReverseCollection does. But then you'd be trading away the convenience of shared index representation for having to unwrap the index when you want it.

@belkadan
Copy link
Contributor

One thing I can think of (given magic constraints) is to conditionalize the warning on using a literal. That'd catch 90% of the problem cases.

@swift-ci
Copy link
Collaborator Author

Comment by chungdan (JIRA)

In my opinion, in this case, the value of subscript should be based on the result of applying the filter. That's what people around me think. Why don't you discuss it? If you think my opinion is not good, how do I get filtered third value in the sequence if I want it? Do I have to use drop??

@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. compiler The Swift compiler in itself standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants