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
Suggestion: Favour fail!
over Option<T>
for error states
#19
Conversation
…alues dont wrap in an Option monad but just fail if the foreign function returns invalid data. I think it is OK to `fail!` instead of option when there is really an error state.
I will be more in favor of change which make: fn new_opt() -> Option<T>;
fn new() -> T; // fail internally I think i would like to keep both constructor, i don't like the idea that a non recoverable fail can come from the library, |
In effect, the fail has allready come from the library, gtk+-3.0 in this case. Also, i'm not sure how a user might respond to a failed Found some info on I agree with you though that non recoverable fail's aren't nice and should be prevented as much as possible. It is a hard balance to strike. |
I don't agree to fail!. Library shouldn't stop because of an internal error if it's not a critical one. |
When |
Yes, critical error inside gtk, but not for the whole program, maybe the developer want to make some log, or printing some debug on standard output. |
He can just pipe stderr, gtk already spews errors there. |
Btw I like your suggestion as a compomise, adding both like this:
The rust std also fails sometimes, like http://doc.rust-lang.org/std/vec/struct.Vec.html#method.get |
Ok, but i want comme back on this, Rust convention give prefix to the case the less predictable or less safe. Option is the right way to handle error in Rust, maybe we should make it the default choice like this: fn new() -> Option<T>;
fn new_unchecked() -> T; // fail internally This is a known pattern in Rust std lib. |
It's a good middle, like that it's totally up to the user. |
I strongly believe we should do it reversed, because:
So I suggest we it like this:
And allow the user the register a callback in case they want to do something in case of failure:
Then, when the user really want the manual null check they can do PS. How do you get github to syntax highlight like that Jeremy :) ? |
I maintain that i prefer that I think that as Users can too define a macro like the // Ok call fail!() if the return is None, else return the window stored in Some()
let mut window = check!(gtk::Window::new()); But it's finally not really different than call To highlight your code just put the Rust flag on your snippet:
|
Ok very well, You can close this issue then. I don't see much point in adding: fn new_unchecked(); Since it is doesnt save keystrokes vs just calling |
When foreign functions are nearly sure to return proper values dont wrap in an Option monad for invalid data but just
fail!
.I think it is OK to
fail!
instead of Option when there is really an error state. The resulting api is also a lot cleaner.