-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] Improved documentation for popLast() & removeLast() in BidirectionalCollection #5333
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
Conversation
/cc @dabrahams and @natecook1000 |
I think a similar note is needed for |
@hooman Good point, documentation has now been updated for both |
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.
These changes are good, but we shouldn't be claiming popLast is safer. It's only preferable because it's guaranteed to be O(1) if it's available.
@dabrahams Ok, I removed the safety claim. |
I'm sorry, but I'm not comfortable with the recommendation either. It seems to me that either |
@dabrahams Safety claim is perhaps my incorrect wording. It is safer in a sense that the type gives you all the info about the behavior of the function, whereas with |
/// You should only use this if you are certain the collection | ||
/// is not empty and performance is an issue. | ||
/// | ||
/// Otherwise opt for `popLast()`. |
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.
I don't want to lose the "must not be empty" sentence. You could rephrase it like this to make a clearer recommendation about when to use popLast()
:
The collection must not be empty. To remove the last element of a collection that might be empty, use the
popLast()
method instead.
/// `popLast()` should generally be preferred over `removeLast()`. | ||
/// | ||
/// You should only opt for `removeLast()` if you are certain the collection | ||
/// is not empty and performance is an issue. |
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.
Could we rewrite this as:
Use
popLast()
to remove the last element of a collection that might be empty. TheremoveLast()
method must be used only on a nonempty collection.
@natecook1000 Ok, I've updated according to your requested changes. |
Sorry for the back and forth on this, the problem turned out to be deeper than it originally looked =) The problem with the comment I originally suggested is: both these methods are not necessarily available in all contexts. You can see that Interestingly, TL;DR: I think the right thing to do now is simply cross-reference the two methods in the documentation. Pretty much the way you've done it at the time of this writing. This case clearly identifies the flaw in the design of these methods, solving which might become a part of a follow-up to this proposal one day. Thanks for working on this, @LouisDhauwe! |
This is sort of weird for a comment only change, but needs to be done. |
@swift-ci Please smoke test and merge |
Added detailed documentation to
popLast()
regarding its safety compared toremoveLast()
,as well as noting the performance cost
popLast()
has.