MaybeResult
Описание
C# implementation of the Maybe and Result monads for better error handling
Языки
- C#100%
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) presents some valid value, 2) 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 and . A non-nullable value must be passed to the method, otherwise the will be thrown.
Also there is implicit conversion of value to , so the code above can be rewritten as:
There are four monadic functions: , , , and .
The Bind function produces a new instance of the Maybe type based on the provided delegate (T -> Maybe<U>) if some value is presented, otherwise returns . It is used for converting an instance of the Maybe type which holds a value of one type to the instance of the Maybe type which holds a value of different type .
The Fold function allows to change the value and pack it to another Maybe instance of the same type. The delegate (T -> Maybe<T>) determines the folder function.
The Iter function allows to execute Action delegate if some value is presented. Returns the instance of the Maybe type which called the function.
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 (T -> U).
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).
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) holds some valid value, 2) holds an error message of what went wrong.
Instances of the Result type can be created with the static methods and . A non-nullable value must be passed to the methods, otherwise the will be thrown.
Also there are implicit conversions of and values to :
There are three monadic functions: , , that work exactly the same as in the Maybe type, and plus three more functions , , (instead of Iter) to execute some Action delegate.
The Error type is a class with immutable properties: and . The Message property can be formatted with parameters if the Error instance was created in the location that differs from the returning location.
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: