-
Notifications
You must be signed in to change notification settings - Fork 10.5k
SR-7266: Avoid nesting of ReversedCollection #15815
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-7266: Avoid nesting of ReversedCollection #15815
Conversation
Hi there, I started looking at this bug last weekend with a solution to the lazy scenario, which you can find at this gist: https://gist.github.com/danielctull/ec521f3fc2aadabb17d38f716e55c5f7 It seems tricky to constrain |
Hi, yes, I also ended up with having to create ReversedCollectionProtocol for the lazy case. It's now updated to the PR. |
Would |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Moximillian, thanks for addressing the issue!
AFAIK @danielctull has tests and properly set availability attributes for the old and new reversed
overloads. It would make a lot of sense for you two to merge efforts.
Let's concentrate on a non-lazy case first.
stdlib/public/core/Reverse.swift
Outdated
@@ -253,6 +253,19 @@ extension ReversedCollection: BidirectionalCollection { | |||
|
|||
extension ReversedCollection: RandomAccessCollection where Base: RandomAccessCollection { } | |||
|
|||
extension ReversedCollection { | |||
/// This is optimization to return identity of doubly reversed collection |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part of the comment is an implementation detail, I don't think it should be a doc-comment.
stdlib/public/core/Reverse.swift
Outdated
// For example [1,2].reversed().reversed() => [1,2] | ||
|
||
/// Returns the elements of the original collection (reversal of reversal) | ||
public func reversed() -> Base { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a more concrete overload of an existing method, we should be careful not to break existing code.
The type of let xs = [1,2,3].reversed().reversed()
changes. This new overload should be @available(swift, introduced: 4.2)
.
As @moiseev said, I have a branch which adds tests and availability attributes which you can see in this comparison. |
My first starterbug so please bear with me :)
|
Hi @Moximillian! I've sent you a more long winded email but to paraphrase here: Because I'm a little vain and I'd like to be able to have recognition for my work, I split my test commit into the regular and lazy checks and rebased that into your branch. You can see this here: https://github.com/danielctull-forks/swift/commits/SR-7266-avoid-nesting-reversed. It keeps all of you other changes, but allows me to be the committer for the test additions. You could either reset my branch or cherry-pick my commit dbdd74a8a8fcf6f70cc9810fe81b5efaa18bd57e and force push to your fork to update this PR. |
Applying reversed() two times should return identity.
768c1c3
to
b859cdc
Compare
Thanks @danielctull, squashed my commits and cherry-picked your test commit. Git is amazing tool, interesting to learn more about it :) So all-in-all, this is now same as before: just adding the non-lazy optimisation for ReversedCollection + tests for it. |
Cool, cheers @Moximillian. I think this line to run the test was lost when I split out the lazy tests earlier, if you want to merge this one in too: 110655befd06952c82f4f28406476da573962d8c I ran these on the previous branch (before you squashed) and it ran fine! 😁 |
Cherry-picked the missing line as well |
@@ -84,7 +82,7 @@ extension ReversedCollection { | |||
} | |||
} | |||
} | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: Unnecessary space at the end of the line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so my change removes the unnecessary space from that line. Or would you rather not have those kind of cleanups in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh. I misinterpreted the diff. I would prefer to not mix the cleanups with functional changes, but am guilty of doing it every now and then myself. So let's keep it.
@swift-ci Please test |
@swift-ci Please Test Source Compatibility |
For the record. The binary size difference is negligible. |
Applying reversed() two times should return original collection.
First round of fixes for the SR-7266, to avoid nesting multiple ReversedCollections and instead return the original collection with two consecutive .reversed() functions.
Does not yet fix the .lazy use case.
Resolves SR-7266.