1

The code for LanguageExt.Common.Result.Map is as follow:

[Pure]
public Result<B> Map<B>(Func<A, B> f) =>
    IsFaulted 
        ? new Result<B>(Exception)
        : new Result<B>(f(Value));

How to manage the scenario where the Func f might throw an error too?

For example, let's say I have this:

public Result<string> DoFirst()
{
    return Result<string>("Good");
}

public Result<int> DoNext()
{
    var result = DoFirst();

    return result.Map<int>(_ =>
    {
        try
        {
            // Some unsafe code that will eventually
            // throw new InvalidOperationException();
            return 200;
        }
        catch(Exception ex)
        {
            // return Result<int>(ex); // Not allowed, must return an int
            // return ex; // Not allowed, must return an int
        }
    });
}

What happens if the Exception is not handled?

It seems like we are missing a FlatMap method or something like that, or May be I am not using the right Class.

######### ADDED #########

FlatMap could look like this:

[Pure]
public Result<B> FlatMap<B>(Func<A, Result<B>> f) =>
    IsFaulted 
        ? new Result<B>(Exception)
        : f(Value);

That would have solved my issue. Should I create a pull request?

acmoune
  • 2,981
  • 3
  • 24
  • 41
  • Did the `Result` come from a `Try` or something like that? You should `Map` *that* instead of consuming the `Result` directly. – Sweeper Aug 30 '23 at 03:42
  • Okay, this is the second time that I have received that recommendation. I will just switch to a `Try`. Thanks. – acmoune Aug 30 '23 at 03:47

0 Answers0