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

Stabilize Result::map_or_else #66322

Merged
merged 2 commits into from
Nov 24, 2019

Conversation

tesuji
Copy link
Contributor

@tesuji tesuji commented Nov 12, 2019

Stabilized this API:

impl<T, E> Result<T, E> {
    pub fn map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
        match self {
            Ok(t) => f(t),
            Err(e) => default(e),
        }
    }
}

Closes #53268
r? @SimonSapin

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 12, 2019
@Centril Centril added relnotes Marks issues that should be documented in the release notes of the next release. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 12, 2019
@Centril Centril added this to the 1.41 milestone Nov 12, 2019
@Centril Centril added the needs-fcp This change is insta-stable, so needs a completed FCP to proceed. label Nov 12, 2019
@SimonSapin
Copy link
Contributor

This matches the signature of already-stable Option::map_or_else (except that the default callback gets the error value as a parameter)

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Nov 12, 2019

Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Nov 12, 2019
@@ -539,9 +538,12 @@ impl<T, E> Result<T, E> {
/// assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 42);
/// ```
#[inline]
#[unstable(feature = "result_map_or_else", issue = "53268")]
pub fn map_or_else<U, M: FnOnce(T) -> U, F: FnOnce(E) -> U>(self, fallback: F, map: M) -> U {
self.map(map).unwrap_or_else(fallback)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was wrong with the previous implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new implementation is consistent with Option::map_or_else: https://doc.rust-lang.org/nightly/src/core/option.rs.html#491-496

So we are following Principle of Least Surprise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even less Surprising would be to not change an implementation in a stabilization PR.

Also, IMO the original implementation was better because it more clearly shows the relation between map_or_else, map, and unwrap_or_else. The whole point of these methods is that we don't have to write matches everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like it, we could send a PR later to fix it in both Option and Result.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@timvermeulen That might be true, but following the paradigm of keeping things simple and consistent is also needed. The original idea was to create map_or_else and unwrap_or_else the same as on Option, and then things get switched around. That is just absurd, and makes it harder to deal with because you to constantly ask yourself, "which way arround was this command?!" - and honestly i dont have time for that in a progeramming language.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomwhoiscontrary I think that question has been asked before, or a derivative thereof. The conclusion was that Options map_or_else is already in stable and would therefore not be changed, as that would be breaking much more.
Part of the discussion is in here #53268

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KatsuoRyuu My question is the one i asked - why is Option::map_or_else the way it is. I am not suggesting changing it. I want to understand why it is the way it is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomwhoiscontrary sorry for the misunderstanding that, will see if I can find the documentation for that for you, I think I saw it somewhere ;)

Copy link

@KatsuoRyuu KatsuoRyuu Nov 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomwhoiscontrary this is the thread I remembered seeing, rust-lang/rfcs#1025. So the reason for the initially is to follow the convention of map_or, so all parameters are always prepended.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The explanation makes sense. If we had keyword arguments, this problem would disappear!

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Nov 12, 2019
@rfcbot
Copy link

rfcbot commented Nov 12, 2019

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot removed the proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. label Nov 12, 2019
@NilsIrl
Copy link

NilsIrl commented Nov 15, 2019

Shouldn't the arguments be the other way round to match those of Option?

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Nov 22, 2019
@rfcbot
Copy link

rfcbot commented Nov 22, 2019

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@tesuji
Copy link
Contributor Author

tesuji commented Nov 23, 2019

r? @dtolnay

@rust-highfive rust-highfive assigned dtolnay and unassigned SimonSapin Nov 23, 2019
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Relatedly, I wish that we had more realistic examples for these type of combinator methods. The current example code is:

let k = 21;

let x : Result<_, &str> = Ok("foo");
assert_eq!(x.map_or_else(|e| k * 2, |v| v.len()), 3);

which is so contrived that it doesn't help you understand how the method is intended to be used.

It would be better to find real uses in the wild and distill them into example code. Something like this is where I would expect to encounter map_or_else:

    ...
    .map_or_else(Response::DatabaseError, |id| {
        if id.is_empty() {
            Response::Empty
        } else {
            Response::User(id.to_string())
        }
    })

@dtolnay
Copy link
Member

dtolnay commented Nov 24, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Nov 24, 2019

📌 Commit c06a8ea has been approved by dtolnay

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 24, 2019
@bors
Copy link
Contributor

bors commented Nov 24, 2019

⌛ Testing commit c06a8ea with merge 7d761fe...

bors added a commit that referenced this pull request Nov 24, 2019
Stabilize Result::map_or_else

Stabilized this API:
```rust
impl<T, E> Result<T, E> {
    pub fn map_or_else<U, D: FnOnce(E) -> U, F: FnOnce(T) -> U>(self, default: D, f: F) -> U {
        match self {
            Ok(t) => f(t),
            Err(e) => default(e),
        }
    }
}
```

Closes #53268
r? @SimonSapin
@bors
Copy link
Contributor

bors commented Nov 24, 2019

☀️ Test successful - checks-azure
Approved by: dtolnay
Pushing 7d761fe to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Nov 24, 2019
@bors bors merged commit c06a8ea into rust-lang:master Nov 24, 2019
@tesuji tesuji deleted the consistent-result-map_or_else branch November 24, 2019 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tracking issue for Result::map_or_else