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

Quick draft "Result::expect" rfc #1119

Merged
merged 1 commit into from
Jun 10, 2015

Conversation

thepowersgang
Copy link
Contributor

See also rust-lang/rust#25359 for implementation

Rendered


# Drawbacks

- It involves adding a new method to a core rust type.
Copy link
Member

Choose a reason for hiding this comment

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

Not a drawback per se.

@bluss
Copy link
Member

bluss commented May 13, 2015

Seems logical to me. After you get to know .expect() on Option, you'll expect to find the method on Result too.

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label May 14, 2015
@Manishearth
Copy link
Member

@gankro @sfackler @huonw This needs a sheriff

@nielsle
Copy link

nielsle commented May 17, 2015

This would be great for quick and dirty scripts. It would also make one of the first examples in the rust book a little easier to read:

   io::stdin().read_line(&mut guess)
        .ok()
        .expect("Failed to read line");

https://doc.rust-lang.org/book/guessing-game.html


# Detailed design

Add a new method to the same `impl` block as `Result::unwrap` that takes a `&str` message and
Copy link
Member

Choose a reason for hiding this comment

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

I dislike this signature, and much prefer:

impl<T, U: Debug, V: Display> Foo<T, V> for Result<T, U> {
    fn expect(self, msg: V) -> T {
        match self {
            Ok(v) => v,
            Err(e) => panic!("Error: {} ({:?})", msg, e)
        }
    }
}

Copy link
Member

Choose a reason for hiding this comment

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

(And I think Option should be the same)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooh, interesting idea. This would allow passing format_args! for formatted messages

@grissiom
Copy link

I think .expect("Failed to read line") mean to be "expect to Fail..." which is logically inverse to what it is done. I think .on_error("Failed to read line") should be more logical. Am I the only one thinking this way?...

@thepowersgang
Copy link
Contributor Author

@grissiom Possibly, this RFC just brings the avaliable methods into line with Option's methods.

@gsingh93
Copy link

@grissiom, I completely agree. I think this should at least be discussed before accepting this RFC so we'd only have to deprecate one method, not two, if this were accepted.

@thepowersgang
Copy link
Contributor Author

@grissiom @gsingh93 The .expect() method is part of the stable API for Option, and thus cannot be changed without incrementing the language's major version.

@gsingh93
Copy link

Yes, but it can be deprecated.

@grissiom
Copy link

@gsingh93 @thepowersgang OK, when I have time, I will open an other RFC to discuss Option::expect and reference this RFC. Thanks.

@alexcrichton alexcrichton added the final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. label Jun 2, 2015
@alexcrichton
Copy link
Member

This RFC is now entering the week-long final comment period.

@emberian
Copy link
Member

emberian commented Jun 2, 2015

I still REALLY think that this should take msg: Display, but that can be added after the fact. I'm happy with this RFC in its entirety (including the format string).

@gkoz
Copy link

gkoz commented Jun 3, 2015

I don't think the point about Debug formatting for errors being inferior to Display has been addressed.

Which of these are more helpful?
Error { repr: Os(2) } vs No such file or directory (os error 2)
JoinPathsError { inner: JoinPathsError } vs path segment contains separator ':'

Maybe this issue should be raised elsewhere though because it's not a problem with expect per se.

@BurntSushi
Copy link
Member

Maybe this issue should be raised elsewhere though because it's not a problem with expect per se.

I suspect this is the right way to think about it. For better or worse, Option::unwrap, Option::expect, Result::unwrap and Result::unwrap_err are all bounded with Debug. Making Result::expect use Display instead seems pretty surprising to me.

@Gankra
Copy link
Contributor

Gankra commented Jun 3, 2015

The text as written now is a bit unclear. I'd really like it to explicitly provide the signature and implementation (since it should be trivial to provide, and is the entire point of the RFC).

@thepowersgang
Copy link
Contributor Author

@gkoz That should be raised somewhere, probably on the offending error type, making Debug include the error text as well as the number.

@sfackler
Copy link
Member

sfackler commented Jun 4, 2015

Maybe this issue should be raised elsewhere though because it's not a problem with expect per se.

Agreed. We could change the conventions for error types to have more informative Debug output, or, when specialization is implemented, use Display output for objects implementing Debug+Display and Debug output for objects only implementing Debug as a couple of options.

@bluss
Copy link
Member

bluss commented Jun 4, 2015

Edit: see below.

@BurntSushi: I think the cases are different. The Debug bound is on the value inside the Result. The Display bound would be on the message you pass to .expect(). Display is a (natural?) generalization of passing just a &str for a message. Debug is not a generalization of passing a string at all.

Also, we can demand a rarer trait like Display when the user must consciously choose to pass in a fitting message. We need Debug for the other cases to be able to work with a broader range of values contained in the Result.

@gkoz
Copy link

gkoz commented Jun 4, 2015

@bluss but that part of discussion was about the value inside the Result, not the message.

@bluss
Copy link
Member

bluss commented Jun 4, 2015

@gkoz Oops! I'm sorry for the noise.

@alexcrichton
Copy link
Member

The consensus among the libs team is to accept this RFC now. The matter of generalizing the argument of expect to T: Display can be handled at a later date (and should also affect Option as well). Additionally, using Debug instead of Display for the error type in the result follows the precedent of the unwrap method, which while without downsides covers the most possible types today.

Thanks for the RFC @thepowersgang!

@Centril Centril added A-panic Panics related proposals & ideas A-error-handling Proposals relating to error handling. labels Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-error-handling Proposals relating to error handling. A-panic Panics related proposals & ideas final-comment-period Will be merged/postponed/closed in ~10 calendar days unless new substational objections are raised. T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

Successfully merging this pull request may close these issues.