Skip to main content

Sundew.DiscriminatedUnions by Kim Hugener Ohlsen

NuGet / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

Author

note

Kim Hugener Ohlsen Alt text

Original Readme

note

Discriminated Unions

Sundew.DiscriminatedUnions implement discriminated unions for C#, until a future version of C# provides it out of the box. The idea is that this package can be deleted once unions are supported in C#, without requiring changes to switch expressions and statements.

In addition, the project supports dimensional unions through default interface methods (traits). A dimensional union is a union where cases can be reused in any number of unions, by supporting interface unions through the possibility of implementing multiple interface and default interface members.

How it works

A Roslyn analyzer asserts and report errors in case switch statements or switch expression do not handle all cases. C# 8 and 9 already comes with great pattern matching support for evaluation.

In order that the inheritance hierarchy remain closed (All cases in the same assembly), an analyzer ensures that unions are not derived from in referencing assemblies. Similarly all case classes should be sealed.

Create a union by inheriting from an abstract base (record) class (or interface) marked with the DiscriminatedUnion attribute to build various cases. Either specify the partial keyword to the union for a source generator to implement factory methods or use the codefix PDU0001 to generate them.

Sample

######### Defining a union

[Sundew.DiscriminatedUnions.DiscriminatedUnion]
public abstract partial record Result
{
public sealed partial record Success : Result;

public sealed partial record Warning(string Message) : Result;

public sealed partial record Error(int Code) : Result;
}

Alternatively, a union can be defined with unnested case classes and interfaces, allowing the possibility of creating dimensional unions (see below).

######### Evaluation

var message = result switch
{
Result.Error \{ Code: > 70 \} error => $"High Error code: {error.Code}",
Result.Error error => $"Error code: {error.Code}",
Result.Warning \{ Message: "Tough warning" \} => "Not good",
Result.Warning warning => warning.Message,
Result.Success => "Great",
};

######### Dimensional unions To support dimensional unions, unnested cases help because the cases are no longer defined inside a union. However, for this to work the unions are required to declare a factory method named exactly like the case type and that has the CaseType attribute specifying the actual type. Since version 3, factory methods are generated when the union is declared partial. Alternatively, a code fix (PDU0001) is available to generate the factory methods.

[Sundew.DiscriminatedUnions.DiscriminatedUnion]
public partial interface IExpression;

[Sundew.DiscriminatedUnions.DiscriminatedUnion]
public partial interface IArithmeticExpression : IExpression;

[Sundew.DiscriminatedUnions.DiscriminatedUnion]
public partial interface ICommutativeExpression : IArithmeticExpression;

public sealed partial record AdditionExpression(IExpression Lhs, IExpression Rhs) : ICommutativeExpression;

public sealed partial record SubtractionExpression(IExpression Lhs, IExpression Rhs) : IArithmeticExpression;

public sealed partial record MultiplicationExpression(IExpression Lhs, IExpression Rhs) : ICommutativeExpression;

public sealed partial record DivisionExpression(IExpression Lhs, IExpression Rhs) : IArithmeticExpression;

public sealed partial record ValueExpression(int Value) : IExpression;

########## Evaluating dimensional unions With dimensional unions it is possible to handle all cases using a sub union. As seen in the example below, handling the ArithmeticExpression covers Addition-, Subtraction-, Multiplication- and DivisionExpression. Typically one would dispatch these to a method handling ArithmeticExpression and where handling all cases would be checked, but it is not required. This makes it convienient to separate handling logic in smaller chucks of code.

public int Evaluate(Expression expression)
{
return expression switch
{
ArithmeticExpression arithmeticExpression => Evaluate(arithmeticExpression),
ValueExpression valueExpression => valueExpression.Value,
};
}

public int Evaluate(ArithmeticExpression arithmeticExpression)
{
return arithmeticExpression switch
{
AdditionExpression additionExpression => Evaluate(additionExpression.Lhs) + Evaluate(additionExpression.Rhs),
SubtractionExpression subtractionExpression => Evaluate(subtractionExpression.Lhs) - Evaluate(subtractionExpression.Rhs),
MultiplicationExpression multiplicationExpression => Evaluate(multiplicationExpression.Lhs) * Evaluate(multiplicationExpression.Rhs),
DivisionExpression divisionExpression => Evaluate(divisionExpression.Lhs) / Evaluate(divisionExpression.Rhs),
};
}

########## Enum evaluation As of version 5.1, regular enums can also use the DiscriminatedUnion attribute causing the analyzer to exhaustively check switch statements and expressions.

Generator features

As mentioned a source generator is automatically activated for generating factory methods when the partial keyword is specified. In addition, the DiscriminatedUnion attribute can specify a flags enum (GeneratorFeatures) to control additional code generation.

  • Segregate - Generates an extension method for IEnumerable<TUnion> that segregates all items into buckets of the different result.
Supported diagnostics:
Diagnostic IdDescriptionCode Fix
SDU0001Switch does not handled all casesyes
SDU0002Switch should not handle default caseyes
SDU0003Switch has unreachable null caseyes
SDU0004Class unions must be abstractyes
SDU0005Only unions can extended other unionsno
SDU0006Unions cannot be extended outside their assemblyno
SDU0007Cases must be declared in the same assembly as their unionsno
SDU0008Cases should be sealedyes
SDU0009Unnested cases should have factory methodPDU0001
SDU0010Factory method should have correct CaseTypeAttributeyes
SDU0011Reported when a case is implemented by throwing NotImplementedException, because CodeCleanup may siliently 'fix' SDU0001.yes
SDU0012Reported when a case contains type parameters that are not in the union type parameter list.yes
PDU0001Make union/case partial for code generatoryes
PDU0002Populate union factory methodsyes
SDU9999Switch should throw in default caseno
GDU0001Discriminated union declaration could not be foundno
Issues/Todos

About

note

Generate tagged union

How to use

Example (source csproj, source files)

This is the CSharp Project that references Sundew.DiscriminatedUnions

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Sundew.DiscriminatedUnions" Version="6.0.0" ></PackageReference>
</ItemGroup>



</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

#nullable enable

namespace UnionTypesDemo
{
#pragma warning disable SA1601
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Sundew.DiscriminateUnions.Generator", "6.0.0.0")]
public partial record ResultSave : global::Sundew.DiscriminatedUnions.IDiscriminatedUnion
#pragma warning restore SA1601
{
/// <summary>
/// Gets the NotFound case.
/// </summary>
/// <returns>The NotFound.</returns>
[Sundew.DiscriminatedUnions.CaseType(typeof(global::UnionTypesDemo.ResultSave.NotFound))]
public static global::UnionTypesDemo.ResultSave _NotFound \{ get; }
= new global::UnionTypesDemo.ResultSave.NotFound();

/// <summary>
/// Factory method for the Ok case.
/// </summary>
/// <param name="i">The i.</param>
/// <returns>A new Ok.</returns>
[Sundew.DiscriminatedUnions.CaseType(typeof(global::UnionTypesDemo.ResultSave.Ok))]
public static global::UnionTypesDemo.ResultSave _Ok(int i)
=> new global::UnionTypesDemo.ResultSave.Ok(i);

/// <summary>
/// Gets all cases in the union.
/// </summary>
/// <returns>A readonly list of types.</returns>
public static global::System.Collections.Generic.IReadOnlyList<global::System.Type> Cases \{ get; }
= new global::System.Type[] \{ typeof(global::UnionTypesDemo.ResultSave.NotFound), typeof(global::UnionTypesDemo.ResultSave.Ok) };
}
}

Useful

Download Example (.NET C#)

Share Sundew.DiscriminatedUnions

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Sundew.DiscriminatedUnions

Category "FunctionalProgramming" has the following generators:

1 cachesourcegenerator Nuget GitHub Repo stars 2024-02-14

2 dunet Nuget GitHub Repo stars 2023-04-16

3 Dusharp Nuget GitHub Repo stars 2024-09-19

4 Funcky.DiscriminatedUnion Nuget GitHub Repo stars 2024-01-18

5 FunicularSwitch NugetNuget GitHub Repo stars 2024-02-12

6 N.SourceGenerators.UnionTypes Nuget GitHub Repo stars 2023-10-29

7 OneOf NugetNuget GitHub Repo stars 2023-08-21

8 PartiallyApplied Nuget GitHub Repo stars 2023-04-16

9 polytype Nuget GitHub Repo stars 2024-11-04

10 rscg_demeter Nuget GitHub Repo stars 2025-03-26

11 rscg_queryables NugetNuget GitHub Repo stars 2024-11-02

12 RSCG_Utils_Memo Nuget GitHub Repo stars 2023-08-27

13 Sera.Union Nuget GitHub Repo stars 2024-08-26

14 Sundew.DiscriminatedUnions Nuget GitHub Repo stars 2026-02-14

15 TypeUtilities Nuget GitHub Repo stars 2024-03-05

16 UnionGen Nuget GitHub Repo stars 2024-04-05

17 UnionsGenerator Nuget GitHub Repo stars 2024-02-18

See category

FunctionalProgramming