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

Extract errors #4

Closed
OtaK opened this issue Sep 20, 2018 · 8 comments
Closed

Extract errors #4

OtaK opened this issue Sep 20, 2018 · 8 comments
Labels

Comments

@OtaK
Copy link

OtaK commented Sep 20, 2018

Hey,

First of all, thanks for the amazing crate!
Is there any way to extract Err(T) in the else statement?

Thanks,
Mathieu

@lambda-fairy
Copy link
Owner

Hi Mathieu, that's good to hear!

What is the code that you're trying to write?

@OtaK
Copy link
Author

OtaK commented Sep 24, 2018

I was wondering if it was possible to write something like that

if_chain! {
    if let Ok(thing) = result_thing;
    then { do stuff with thing; }
    else(my_error) { do stuff with the Err(e) aliased to my_error } 
}

The most bothering thing about if_chain is the fact that once the chain goes to else there's no way to know what/when it went wrong along the chain, making debugging quite hard.

@lambda-fairy
Copy link
Owner

In that case, you can use a normal match:

match result_thing {
    Ok(thing) => ... ,
    Err(my_error) => ... ,
}

That won't work if there are multiple steps of pattern matching involved (what if_chain was designed for), but I don't think there's an easy way to handle the else in that case.

@OtaK
Copy link
Author

OtaK commented Sep 25, 2018

That would somewhat defeat the purpose of using the if_chain macro in the first place though...
Any idea on how it could be done by modifying it?

@lambda-fairy
Copy link
Owner

lambda-fairy commented Sep 25, 2018

You tell me ;)

In this code snippet:

let result_thing: Result<String, MyError>;
if_chain! {
    if let Ok(thing) = result_thing;
    if thing.len() > 2;
    then { /* ... */ }
    else(foo) { /* ... */ }
}

What do you want the type of foo to be?

@OtaK
Copy link
Author

OtaK commented Sep 25, 2018

I think it would be MyError; what so you think?

@lambda-fairy
Copy link
Owner

Then what do we do when result_thing is Ok, but thing.len() <= 2?

@OtaK
Copy link
Author

OtaK commented Sep 27, 2018

Hmm, I see what you mean, then I guess it's simply not possible to do so 🤔
We might send a () but it'll break match arms return types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants