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

Mapping a failure to success #30

Closed
badvok666 opened this issue Aug 20, 2020 · 3 comments
Closed

Mapping a failure to success #30

badvok666 opened this issue Aug 20, 2020 · 3 comments

Comments

@badvok666
Copy link

This is probably not an issue. Its more of a query i have. I very well could be misusing, misunderstanding or not aware of something.

The use case is if the api call fails i want it to successfully return the cache.

Much like andThen allows the transforming of one thing to another contained withing the Result type. What i feel i am looking for is a way to "andThen the error". So i can transform the E into a success using Ok(SomeType)

I can't find the function though i could just be using it wrong/this is not the correct practice.
For now i'm letting my Error contain any type so it can optionally return the result i want.

Thanks in advance, really loving using kotlin-result so far.

@michaelbull
Copy link
Owner

I think you want recover

It would go something like:

class Good( ... )
class Bad(... )

fun makeCall() : Result<Good, Bad> = { ... }

fun caller() {
   val result: Ok<Good> = makeCall().recover { error -> Good(...)  }
}

@badvok666
Copy link
Author

I think that it is.

Thanks!!, Totally loving this and am in the process of replacing sealed classes. I do not miss the in-lining of the when()s

@michaelbull
Copy link
Owner

👍 let me know if you need help getting unblocked again.

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

No branches or pull requests

2 participants