https://github.com/ricardotondello/functional.discriminatedunion
Functional.DiscriminatedUnion is a C# library that provides a lightweight implementation of discriminated unions, also known as tagged unions or sum types, for functional programming in C#.
https://github.com/ricardotondello/functional.discriminatedunion
csharp discriminated-unions dotnet dotnetcore extensions
Last synced: 4 months ago
JSON representation
Functional.DiscriminatedUnion is a C# library that provides a lightweight implementation of discriminated unions, also known as tagged unions or sum types, for functional programming in C#.
- Host: GitHub
- URL: https://github.com/ricardotondello/functional.discriminatedunion
- Owner: ricardotondello
- License: mit
- Created: 2024-05-01T12:07:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-25T20:36:33.000Z (8 months ago)
- Last Synced: 2025-06-20T23:04:48.093Z (4 months ago)
- Topics: csharp, discriminated-unions, dotnet, dotnetcore, extensions
- Language: C#
- Homepage:
- Size: 32.2 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
# 🍊🍏 Functional Discriminated Union
[](https://github.com/ricardotondello/Functional.DiscriminatedUnion/actions/workflows/dotnet.yml)
[](https://sonarcloud.io/dashboard?id=ricardotondello_Functional.DiscriminatedUnion)
[](https://sonarcloud.io/component_measures?id=ricardotondello_Functional.DiscriminatedUnion&metric=coverage)
[](https://nuget.org/packages/Functional.DiscriminatedUnion)
[](https://www.nuget.org/packages/Functional.DiscriminatedUnion)Functional.DiscriminatedUnion is a C# library that provides a lightweight implementation of discriminated unions, also known as tagged unions or sum types, for functional programming in C#.
## Overview
Discriminated unions are a powerful concept in functional programming that allow you to define a type that can hold values of several different types. Each value is tagged with its type, allowing for pattern matching and type-safe handling of these values.## Features
- **Lightweight Implementation**: The library provides a minimalistic implementation of discriminated unions, keeping the syntax simple and easy to use.
- **Type Safety**: Discriminated unions enforce type safety at compile time, preventing runtime errors due to mismatched types.
- **Pattern Matching**: Pattern matching is supported for extracting values from the discriminated unions, enabling elegant and concise code.## Installation 🚀
To use Functional.DiscriminatedUnion in your C# project, you can install it via NuGet Package Manager:
```powershell
dotnet add package Functional.DiscriminatedUnion
```## Usage 🛠️
The library defines the OneResult class, which represents a discriminated union of one or more types. Here's a brief overview of the available classes:
- **OneResult**: Represents a discriminated union with one type.
- **OneResult**: Represents a discriminated union with two types.
- **OneResult**: Represents a discriminated union with three types.
- **OneResult**: Represents a discriminated union with four types.**Usage:**
```csharp
public record UserCreated(int Id, string Name){}
public record UserUpdated(int Id, string Name, DateTime UpdatedAt){}public OneResult CreateOrUpdateUser()
{
if (!exists)
{
return new UserCreated(1, "User Name"); // Implicit conversion to a discriminated union
}return new UserUpdated(1, "User Name changed", DateTime.UtcNow); // Implicit conversion to a discriminated union
}var result = CreateOrUpdateUser();
var output = result.Match(
value => $"Result is a UserCreated: {value}",
value => $"Result is a UserUpdated: {value}"
); // Pattern matching to extract value based on its typeUserCreated userCreated = result.AsT1();
Console.WriteLine($"Id: {userCreated.Id}, Name: {userCreated.Name}");//TryGetValue
if (result.TryGetT1(out UserCreated userCreated)
{
Console.WriteLine($"result is a UserCreated");
}
else
{
Console.WriteLine($"result is something else!");
}//Actionable
result.When(
actUserCreated =>
{
DoSomething(actUserCreated);
},
actUserUpdated =>
{
DoSomethingElse(actUserUpdated);
});
```## Contributing 👥
Contributions are welcome! If you find a bug or have a feature request, please open an issue on GitHub.
If you would like to contribute code, please fork the repository and submit a pull request.## License 📄
This project is licensed under the MIT License.
See [LICENSE](https://github.com/ricardotondello/Functional.DiscriminatedUnion/blob/main/LICENSE) for more information.## Support ☕