A Result monad implementation in .NET. Build more resilient applications by being explicit with what errors can be returned from your methods.
Install the NuGet package:
dotnet add package LSymds.Result
You create results by calling Success
or Erroneous
on the Result<TSuccess, TError>
type which builds a result
that is in a successful or erroneous state respectively.
For example, to create a successful Result<string, MyErrorCode>
:
return Result<string, MyErrorCode>.Successful("Success!");
and to create an erroneous Result<string, MyErrorCode>
:
return Result<string, MyErrorCode>.Erroneous(MyErrorCode.Broken);
To keep this library and any implementing code simple and easy to understand, I made the decision to implement one
consistent interface (Result<TSuccess, TError>
) regardless of whether TSuccess
was a value of any substance or not.
As C# does not contain a Nothing
type, this library provides one.
If you wish to return a result with a 'void' success type, you can do the following:
return Result<Nothing, MyErrorCode>.Successful(Nothing.Instance);
Numerous utility methods hang off of the Result<TSuccess, TError>
type.
Map
- Returns a new result with the success type as the provided generic type, mapping the existing data value using the provided function and leaving the error value (if present) unchanged.MapError
- Performs similarly to Map, except it maps the error type and error value, leaving the data value (if present) unchanged.Unwrap
- UNSAFELY retrieves the value ofData
, throwing anInvalidOperationException
if the result is erroneous.UnwrapOrElse
- Retrieves the value ofData
if the result is successful or the provided value if it isn't.