Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bmtlab/stateresults

A redesigned version of the Unions functional paradigm, inspired by the OneOf package
https://github.com/bmtlab/stateresults

csharp discriminated-unions oneof results state unions

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

# BMTLab.StateResults
[![NuGet](https://img.shields.io/nuget/v/BMTLab.StateResults?logo=nuget)](https://www.nuget.org/packages/BMTLab.StateResults)
![Nuget Downloads](https://img.shields.io/nuget/dt/BMTLab.StateResults)
![.NET](https://github.com/BMTLab/StateResults/workflows/publish/badge.svg)

## 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
```csharp
OneOf union;

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

union = 42;
Console.WriteLine(union); // >> 42
```

2. Equality Check
```csharp
OneOf unionA = 42;
OneOf unionB = 42;

bool isMatch = unionA == unionB;
Console.WriteLine(isMatch); // >> True
```

3. Matching Values
```csharp
OneOf union = "Some text";

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

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