Skip to content

wdolek/w4k-either

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

W4k.Either /ˈaɪ.ðər/

W4k.Either Build NuGet Version NuGet Version

Why?

There are several ways to handle the results of operations in C#. For instance, you could:

  • Throw an exception: However, this can be costly and isn't always appropriate. Exceptions can be thought of as goto commands within the entire call stack.
  • Return a tuple: While this method is feasible, it is not very descriptive and can clutter your code.
  • Write a unique result type for every operation: This approach requires significant effort, and we know developers prefer efficiency.

A more practical solution is to use this project.

What?

This project is source for two packages:

  • W4k.Either: Predefined types that can be used to represent discriminated union.
  • W4k.Either.CodeGeneration: Provides source generator that can be used to generate your own types with custom logic/rules.

Please follow links above to get more information about each package.

Example of Either<TLeft, TRight>:

Either<User, FetchError> GetUser(UserIdentifier userId)
{
    try
    {
        // ...
        return new User(...);
    }
    catch (Exception ex)
    {
        return new FetchError(ex);
    }
}
var result = GetUser(userId);
var message = result.Match(
    user => $"User {user.Name} fetched",
    error => $"Error {error.Message} occured"
);

Types provided by library

Type Description
Either<TLeft, TRight> Discriminated union of two or more types
Maybe<TValue> Optional value representation
Result<TError> Result representation without a value
Result<TValue, TError> Result representation with a value
OptionalResult<TValue, TError> Optional result with a value (similar to Result<Maybe, TError>)

More details about each type can be found here.

Generating own types

You can generate your own types with custom logic/rules:

[Either]
public readonly partial struct Gelf<TCamille, TCat, TCrichton>
{
}

Please follow this link to get more information.

Alternative/similar packages

Listed alphabetically

  • Ardalis.Result: A simple implementation of Result for C#
  • CSharpFunctionalExtensions: Functional extensions for C#
  • Danom: Danom is a C# library that provides (monadic) structures...
  • dunet: Simple source generator for discriminated unions in C#
  • ErrorOr: A simple, fluent discriminated union of an error or a result
  • FluentResults: A lightweight .NET library to handle errors and failures in a fluent way
  • FxKit: A library for C# to enable functional, railway-oriented programming using common abstract data types
  • LanguageExt: Provides functional-programming "base class" library
  • Nut.Result: Provides an object in .NET that represents the result of a simple process
  • OneOf: Discriminated unions for C#
  • Optional: A robust option/maybe type for C#
  • Result.Net: A simple wrapper over an operation execution results to indicate success or failure
  • SimpleResults: A simple library to implement the Result pattern for returning from services

Additional resources

Technical notes describing possible states of monad regarding to C# type system, here.


Shapes and symbols icons created by Freepik - Flaticon

About

Yet another choice monad implementation

Resources

License

Stars

Watchers

Forks

Packages

No packages published