Skip to content

Quick draft "Result::expect" rfc - #1119

Merged
alexcrichton merged 1 commit into
rust-lang:masterfrom
thepowersgang:result-expect
Jun 10, 2015
Merged

Quick draft "Result::expect" rfc#1119
alexcrichton merged 1 commit into
rust-lang:masterfrom
thepowersgang:result-expect

Conversation

@thepowersgang

Copy link
Copy Markdown
Contributor

See also rust-lang/rust#25359 for implementation

Rendered

Copy link
Copy Markdown
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

bluss commented May 13, 2015

Copy link
Copy Markdown

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
Copy Markdown
Member

@gankro @sfackler @huonw This needs a sheriff

@nielsle

nielsle commented May 17, 2015

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
Contributor Author

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

@gsingh93

Copy link
Copy Markdown

@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
Copy Markdown
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
Copy Markdown

Yes, but it can be deprecated.

@grissiom

Copy link
Copy Markdown

@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
Copy Markdown
Member

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

@emberian

emberian commented Jun 2, 2015

Copy link
Copy Markdown
Contributor

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

gkoz commented Jun 3, 2015

Copy link
Copy Markdown

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
Copy Markdown
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

Gankra commented Jun 3, 2015

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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

sfackler commented Jun 4, 2015

Copy link
Copy Markdown
Member

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

bluss commented Jun 4, 2015

Copy link
Copy Markdown

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

gkoz commented Jun 4, 2015

Copy link
Copy Markdown

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

@bluss

bluss commented Jun 4, 2015

Copy link
Copy Markdown

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

@alexcrichton

Copy link
Copy Markdown
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.