Stream: general

Topic: Fully unwrap results


gnzlbg (Dec 05 2019 at 16:13, on Zulip):

Is there a way to fully unwrap results ?e.g. if I build a Result<Result<Result<Result< in generic code, is there a macro or crate that allows ? to unwrap them all at once returning the first error ?

simulacrum (Dec 05 2019 at 16:14, on Zulip):

afaik, nothing existing

simulacrum (Dec 05 2019 at 16:14, on Zulip):

I think it would be plausibly not too hard to implement, though not sure -- seems like either not too hard or pretty much impossible :)

gnzlbg (Dec 05 2019 at 16:19, on Zulip):

I don't think ? would work, because it can only return one error type, right ?

gnzlbg (Dec 05 2019 at 16:20, on Zulip):

We would need to construct an enum of all error types in the chain, and return that as an error, to be able to use ?.

centril (Dec 05 2019 at 16:21, on Zulip):

@gnzlbg do you want μ : T (T a) -> T a ?

centril (Dec 05 2019 at 16:22, on Zulip):

these "flatten maximally" are probably not doable without specialization

gnzlbg (Dec 05 2019 at 16:24, on Zulip):
fn foo() -> Result<T, A | B | C> {
    let x: Result<Result<Result<T, A>, B>, C> = ....:
    x?
}
centril (Dec 05 2019 at 16:25, on Zulip):

maybe you can use frunk for "structural coproducs"

centril (Dec 05 2019 at 16:25, on Zulip):

to achieve A | B | C

gnzlbg (Dec 05 2019 at 16:26, on Zulip):

thanks, that solves half of the problem at least

gnzlbg (Dec 05 2019 at 16:27, on Zulip):

I can use specialization to recurse through the Results as well

gnzlbg (Dec 05 2019 at 16:27, on Zulip):

and use that coproduct for the error type

gnzlbg (Dec 05 2019 at 16:28, on Zulip):

but i don't think I can compute the type of the coproduct easily, I'd need to manually type it in

centril (Dec 05 2019 at 16:32, on Zulip):

have a look at https://docs.rs/frunk/0.3.0/frunk/coproduct/index.html

gnzlbg (Dec 05 2019 at 16:34, on Zulip):

yes, that's what I checked out

gnzlbg (Dec 05 2019 at 16:34, on Zulip):

Maybe if I get all my error types to implement a particular trait, I can use that to merge them.

XAMPPRocky (Dec 05 2019 at 18:48, on Zulip):

@gnzlbg You could use ? as long as your return error type is something that impls From<A>, From<B>, and From<C>.

gnzlbg (Dec 05 2019 at 18:49, on Zulip):

Wouldn't I need to use ??? ?

XAMPPRocky (Dec 05 2019 at 18:49, on Zulip):

Yes, that would be the compromise.

XAMPPRocky (Dec 05 2019 at 18:52, on Zulip):

Personally I've started to grow on using snafu for everything, which lets you express ??? with more context like the following

result.context(error::Http)?.context(error::Database)?.context(error::InvalidUserData)?
Jake Goulding (Dec 06 2019 at 02:25, on Zulip):

@gnzlbg https://stackoverflow.com/questions/52453180/how-do-i-unwrap-an-arbitrary-number-of-nested-option-types

Jake Goulding (Dec 06 2019 at 02:26, on Zulip):

Should be able to apply either answer to Result

XAMPPRocky (Dec 06 2019 at 08:33, on Zulip):

@Jake Goulding Wouldn't you have to have map_err to use and_then to flatten it to a single type?

Jake Goulding (Dec 06 2019 at 14:40, on Zulip):

Mmm, probably.

Last update: Jun 04 2020 at 18:40UTC