DiscriminatedUnion.CS 0.0.1-alpha

This is a prerelease version of DiscriminatedUnion.CS.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package DiscriminatedUnion.CS --version 0.0.1-alpha
                    
NuGet\Install-Package DiscriminatedUnion.CS -Version 0.0.1-alpha
                    
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="DiscriminatedUnion.CS" Version="0.0.1-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DiscriminatedUnion.CS" Version="0.0.1-alpha" />
                    
Directory.Packages.props
<PackageReference Include="DiscriminatedUnion.CS" />
                    
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 DiscriminatedUnion.CS --version 0.0.1-alpha
                    
#r "nuget: DiscriminatedUnion.CS, 0.0.1-alpha"
                    
#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 DiscriminatedUnion.CS@0.0.1-alpha
                    
#: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=DiscriminatedUnion.CS&version=0.0.1-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=DiscriminatedUnion.CS&version=0.0.1-alpha&prerelease
                    
Install as a Cake Tool

Discriminated Union

A library that provides functionality to define a Discriminated Union in C#.

Define some types

public class Success
{
    public Success(double value)
    {
        Value = value;
    }

    public double Value { get; }
}

public class Error
{
    public Error(string message)
    {
        Message = message;
    }

    public string Message { get; }
}

Define union type

Union type must be an abstract, partial class.

Every implementation of IUnionWith<> interface adds a type to union.

public abstract partial class Result : IUnionWith<Success>, IUnionWith<Error> { }

Use the union type

public class Program
{
    public static void Main(string[] args)
    {
        var result = GetRoot(-1);
        var outputMessage = result switch
        {
            Result.Success s => s.Value.ToString(CultureInfo.InvariantCulture),
            Result.Error e => e.Message
        };
        
        Console.WriteLine(outputMessage);
    }   

    public static Result GetRoot(double value)
    {
        return value switch
        {
            < 0 => Result.Error.Create("Value cannot be less than zero"),
            _ => Result.Success.Create(Math.Sqrt(value))
        };
    }
}

Limitations

  • Generic types not supported
  • Generic method mot supported
  • Union one type multiple times not supported
  • T.b.a
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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
0.0.8-alpha 259 4/22/2022
0.0.7-alpha 221 4/21/2022
0.0.6-alpha 221 4/19/2022
0.0.5-alpha 237 4/19/2022
0.0.4-alpha 210 4/18/2022
0.0.3-alpha 203 4/17/2022
0.0.2-alpha 228 3/2/2022
0.0.1-alpha 222 2/28/2022