result-flow icon indicating copy to clipboard operation
result-flow copied to clipboard

Support a method that returns either Err or Ok Result if original Result is Err?

Open davidxia opened this issue 8 years ago • 1 comments

mapErr()

returns the result of the given @{link Function}, wrapped in a new Err Result instance. Otherwise returns this

I'd like to return a Result instance that could be either Ok or Err. Is there any interest in adding this?

davidxia avatar Aug 31 '17 01:08 davidxia

@davidxia I don't really think you should ever need to do that. If you have a function like that, then it would have some some unnecessary complexity inside. You should string your map() and mapErr() calls together to handle the results like so:

Result<String, Exception> result = functionThatProducesResult();
result.map((value) -> {
        // Do something with value
        return value;
    }).mapErr((err) -> {
        // Do something with err 
        return err;
    });

TimelyToga avatar Feb 22 '19 01:02 TimelyToga