Skip to content

A redesigned version of the Unions functional paradigm, inspired by the OneOf package

License

Notifications You must be signed in to change notification settings

BMTLab/StateResults

Repository files navigation

BMTLab.StateResults

NuGet Nuget Downloads .NET

OneOf.Reduced & StateResults - .NET 8 Discriminated Type Unions Library

This library introduces a powerful mechanism for discriminated type unions in .NET 8 through two main constructs: OneOf and Result/Results. Both these constructs can work with up to 6 type arguments (T0, T1, ... T5).

How to Use

OneOf.Reduced

The core concept of the library. Represents a choice between one of the given types.

  1. Basic usage
OneOf<string, int> union;

union = "Operation is successful";
Console.WriteLine(union); // >> Operation is successful

union = 42;
Console.WriteLine(union); // >> 42
  1. Equality Check
OneOf<bool, int, string> unionA = 42;
OneOf<bool, int, string> unionB = 42;

bool isMatch = unionA == unionB;
Console.WriteLine(isMatch); // >> True
  1. Matching Values
OneOf<int, string, Exception> union = "Some text";

string result = union.Match
(
    number => number.ToString(),
    text => text.ToUpperInvariant(),
    error => error.Message
);

Console.WriteLine(result); // >> SOME TEXT