A simple F#'s monads port for C#.
This library contains the main F#'s monads found on FSharp.Core lib written in, and adapted for, C#. Check the docs at Moonad.NET.
The project's package can be found on Nuget and installed by your IDE or shell as following:
dotnet add package Moonador
PM> Install-Package MoonadSince our main goal is to protect the user from NullReferenceException we strongly recommend the use of Nullable Reference Types on any project which uses this lib.
F# offers in it's core library four monads to help you to have more flexibility when working with primitives and also potential null occurrences. So this library do the same.
Also known as Either in some languages this monad offers you the possibility to choose one of two types to be hold by its instance.
Example:
public Choice<int, string> Choose(bool returnInt)
{
if(returnInt)
return 1;
return "This is a Choice!";
}A type to express the final state of a given processing revealing its success of failure and optionally carrying a value or an error.
Example 1 - Success indicator:
public Result Send(Message message)
{
try
{
...
return Result.Ok();
}
catch(Exception exc)
{
...
return Result.Error();
}
}Example 2 - Value and error returning:
public Result<User, IError> Create(...)
{
//When a guard clause is actioned
return new EmptyUserNameError();
//When all is valid
return new User(...);
}This monad, also known as Maybe, has as its goal preventing the NullReferenceException by notifying the existence or absense of a value. Once a potentially null, or simply absent, value is converted to Option it's evaluated to a Some instance, which carry the value, or a None instance, which replaces the null and let the client works as null doesn't exists.
Example 1 - Preventing null from a 3rd party lib:
//lib.Method returns a string
var option = lib.Method().ToOption();
//The ToOption method will turn a null value into a None instance.
if(option.IsSome)
Console.WriteLine($"Returned value: {option}");
if(option.IsNone)
Console.WriteLine($"No returned value.");Example 2 - Creating an Option explicitly:
public Option<int> ReturnWhenGreaterThanZero(int input) =>
input > 0 ? input : Option.None<T>;It has the very same concept as Option but is intended to use with value types to be faster in performance critical scenarios.
Example 1 - Preventing null from a 3rd party lib:
//lib.Method returns a nullable int
var option = lib.Method().ToValueOption();
//The ToOption method will turn a null value into a None instance.
if(option.IsSome)
Console.WriteLine($"Returned value: {option}");
if(option.IsNone)
Console.WriteLine($"No returned value.");Example 2 - Creating an Option explicitly:
public ValueOption<int> ReturnWhenGreaterThanZero(int input) =>
input > 0 ? input : ValueOption<int>.None;