Functional.DiscriminatedUnion 1.1.1

dotnet add package Functional.DiscriminatedUnion --version 1.1.1
                    
NuGet\Install-Package Functional.DiscriminatedUnion -Version 1.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Functional.DiscriminatedUnion" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Functional.DiscriminatedUnion" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Functional.DiscriminatedUnion" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Functional.DiscriminatedUnion --version 1.1.1
                    
#r "nuget: Functional.DiscriminatedUnion, 1.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Functional.DiscriminatedUnion@1.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Functional.DiscriminatedUnion&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Functional.DiscriminatedUnion&version=1.1.1
                    
Install as a Cake Tool

🍊🍏 Functional Discriminated Union

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:

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<T1>: Represents a discriminated union with one type.
  • OneResult<T1, T2>: Represents a discriminated union with two types.
  • OneResult<T1, T2, T3>: Represents a discriminated union with three types.
  • OneResult<T1, T2, T3, T4>: Represents a discriminated union with four types.

Usage:

public record UserCreated(int Id, string Name){}
public record UserUpdated(int Id, string Name, DateTime UpdatedAt){}

public OneResult<UserCreated, UserUpdated> 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 type

UserCreated 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);
    });
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.1 140 6/26/2024
1.1.0 118 5/9/2024
1.0.0 114 5/1/2024