MaybeResult 1.2.0

dotnet add package MaybeResult --version 1.2.0                
NuGet\Install-Package MaybeResult -Version 1.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MaybeResult" Version="1.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MaybeResult --version 1.2.0                
#r "nuget: MaybeResult, 1.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MaybeResult as a Cake Addin
#addin nuget:?package=MaybeResult&version=1.2.0

// Install MaybeResult as a Cake Tool
#tool nuget:?package=MaybeResult&version=1.2.0                

MaybeResult

C# implementation of the Maybe and Result monads for better error handling.

How to build

The project is a class library that targets .NET Standard 2.0. It can be build running the scripts "build.ps1" on Windows or "build.sh" on Linux. The "artifacts" folder contains already compiled binaries.

How to use

Maybe

The Maybe type has two inheritors: 1) Some<T> presents some valid value, 2) None<T> means that there is no any value. The types have internal constructors to ensure that there will not be any other inherited types and thus emulate Discriminated Unions from F#.

Instances of the Maybe type can be created with the static methods Some(T value) and None(). A non-nullable value must be passed to the Some(T value) method, otherwise the ArgumentNullException will be thrown.

Maybe<string> text = Maybe<string>.Some("Hello");
// or
Maybe<string> text = Maybe<string>.None();

// ArgumentNullException
Maybe<string> text = Maybe<string>.Some(null);

Also there is implicit conversion of T value to Maybe<T>, so the code above can be rewritten as:

Maybe<string> text = "Hello";

There are four monadic functions: Bind, Fold, Iter, and Map.

The Bind function produces a new instance of the Maybe type based on the provided delegate Func<T, Maybe<U>> (T → Maybe<U>) if some value is presented, otherwise returns None<U>. It is used for converting an instance of the Maybe type which holds a value of one type T to the instance of the Maybe type which holds a value of different type U.

Maybe<int> number = 5;
Maybe<string> text = number.Bind(x => Maybe<string>.Some(x.ToString()));

The Fold function allows to change the value and pack it to another Maybe instance of the same type. The delegate Func<T, Maybe<T>> (T → Maybe<T>) determines the folder function.

Maybe<int> number = 5;
Maybe<int> newNumber = number.Fold(old => old + 2);

The Iter function allows to execute Action delegate Action<T> if some value is presented. Returns the instance of the Maybe type which called the function.

Maybe<int> number = 5;
number.Iter(x => Console.WriteLine(x));

The Map function allows to map a Maybe instance of one type to Maybe instance of different type. It is similar to the Bind function, but differs in the delegate signature Func<T, U> (T → U).

Maybe<int> number = 5;
Maybe<string> text = number.Map(x => x.ToString());

There is also a non-generic class Maybe that holds a bunch of static Bind and Map functions for multiple parameter cases. The class is abstract and can be extended in the client application if the number of generic parameters is not enough (it is 5 by default).

Maybe<string> name = "Mickey Mouse";
Maybe<int> age = 94;
Maybe<Character> character = Maybe.Map(name, age, (x, y) => new Character(x, y));

Result

The Result type is similar to the Maybe type, but also holds an error message if the value can't be constructed. It has two inheritors: 1) Success<T> holds some valid value, 2) Failure<T> holds an error message of what went wrong.

Instances of the Result type can be created with the static methods Success(T value) and Failure(Error error). A non-nullable value must be passed to the methods, otherwise the ArgumentNullException will be thrown.

Result<string> text = Result<string>.Success("Hello");
// or
Result<string> text = Result<string>.Failure(new Error(
    "Error.MinLengthRequirement",
    "The string must be at least 8 characters long"));

// ArgumentNullException
Result<string> text = Result<string>.Success(null);
Result<string> text = Result<string>.Failure(null);

Also there are implicit conversions of T and Error values to Result<T>:

Result<string> text = "Hello";
Result<string> text = new Error("Error.MinLengthRequired", "The string must be at least 8 characters long");

There are three monadic functions: Bind, Fold, Map that work exactly the same as in the Maybe type, and plus three more functions OnSuccess, OnFailure, OnBoth (instead of Iter) to execute some Action delegate.

Result<string> name = "Mickey Mouse";
Result<int> age = 94;
Result<Character> character = Result.Map(name, age, (x, y) => new Character(x, y))
    .OnSuccess(c => Console.WriteLine(c.ToString()))
    .OnFailure(error => Console.WriteLine(error.ToString()))
    .OnBoth(() => Console.WriteLine("Some message whether it succeded or failed"));

The Error type is a class with immutable properties: Code and Message. The Message property can be formatted with parameters if the Error instance was created in the location that differs from the returning location.

public static class DomainErrors
{
    public static Error MinLengthRequired = new Error(
        "Error.MinLengthRequired",
        "The string '{0}' must be at least {1} characters long");
}

public static Result<Character> Create(string name, int age)
{
    ...
    // Returns "The string 'name' must be at least 8 characters long"
    return DomainErrors.MinLengthRequired.GetFormattedError(nameof(name), 8);
    ...
}

Besides there is the HttpError that adds the integer property StatusCode. These two error types can be inherited to construct other error types.

For more examples see the sample project MaybeResult.Sample:

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 57 6/12/2024