Transform
Methods that take a Result and give you back a Result.
Every method here returns a Result, so the chain continues. To end it, see Consume.
Map
Result<TOut, TErr> Map<TOut>(Func<TOk, TOut> map)Applies a transformation to the success value.
Result<string, string> nameResult = Result.Ok<string, string>("Consent");
Result<int, string> lengthResult = nameResult.Map(name => name.Length);On an Err: the delegate never runs, and the error passes through untouched.
MapErr
Result<TOk, TOut> MapErr<TOut>(Func<TErr, TOut> map)The counterpart. Transforms the error, leaving the success value alone. Reach for it when two pieces of code disagree about the error type and you need them to chain.
Result<int, Error> lengthResult = RollForName() // Result<string, string>
.MapErr(message => new Error("name.failed", message)) // Result<string, Error>
.AndThen(name => CountRunes(name)); // Result<int, Error>On an Ok: the delegate never runs.
AndThen
Chains a step that itself returns a Result. It performs the same operation as And, for each lazily evaluated function.
On an Err: short-circuits. Later steps never run.
Map followed by Flatten does the same thing in two calls. Prefer AndThen.
And
Gives you the first Err, or the last Ok.
Ok1
Ok2
Ok2
Ok
Err
Err
Err
Ok
Err
Err1
Err2
Err1
Evaluated eagerly. If the argument is the result of a function call, use AndThen.
Or
Gives you the first Ok, or the last Err.
Ok1
Ok2
Ok1
Ok
Err
Ok
Err
Ok
Ok
Err1
Err2
Err2
Evaluated eagerly. Use OrElse if the argument costs something.
OrElse
The same as Or, lazily. This is how you recover — and note the factory takes the error, not the success value.
On an Ok: the factory never runs.
Last updated
Was this helpful?