Sundew.DiscriminatedUnions
5.2.8
See the version list below for details.
dotnet add package Sundew.DiscriminatedUnions --version 5.2.8
NuGet\Install-Package Sundew.DiscriminatedUnions -Version 5.2.8
<PackageReference Include="Sundew.DiscriminatedUnions" Version="5.2.8" />
paket add Sundew.DiscriminatedUnions --version 5.2.8
#r "nuget: Sundew.DiscriminatedUnions, 5.2.8"
// Install Sundew.DiscriminatedUnions as a Cake Addin #addin nuget:?package=Sundew.DiscriminatedUnions&version=5.2.8 // Install Sundew.DiscriminatedUnions as a Cake Tool #tool nuget:?package=Sundew.DiscriminatedUnions&version=5.2.8
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 record Result
{
public sealed record Success : Result;
public sealed record Warning(string Message) : Result;
public sealed 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 record AdditionExpression(IExpression Lhs, IExpression Rhs) : ICommutativeExpression;
public sealed record SubtractionExpression(IExpression Lhs, IExpression Rhs) : IArithmeticExpression;
public sealed record MultiplicationExpression(IExpression Lhs, IExpression Rhs) : ICommutativeExpression;
public sealed record DivisionExpression(IExpression Lhs, IExpression Rhs) : IArithmeticExpression;
public sealed 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 Id | Description | Code Fix |
---|---|---|
SDU0001 | Switch does not handled all cases | yes |
SDU0002 | Switch should not handle default case | yes |
SDU0003 | Switch has unreachable null case | yes |
SDU0004 | Class unions must be abstract | yes |
SDU0005 | Only unions can extended other unions | no |
SDU0006 | Unions cannot be extended outside their assembly | no |
SDU0007 | Cases must be declared in the same assembly as their unions | no |
SDU0008 | Cases should be sealed | yes |
SDU0009 | Unnested cases should have factory method | PDU0001 |
SDU0010 | Factory method should have correct CaseTypeAttribute | yes |
PDU0001 | Populate union factory methods | yes |
SDU9999 | Switch should throw in default case | no |
GDU0001 | Discriminated union declaration could not be found | no |
Issues/Todos
- Switch appears with red squiggly lines in VS: https://github.com/dotnet/roslyn/issues/57041
- Nullability is falsely evaluated when the switch hints null is possible: https://github.com/dotnet/roslyn/issues/57042
- SDU0009 gets reported in VS, but no code fix is offered. VS issue here: https://github.com/dotnet/roslyn/issues/57621
- Workaround: A PDU0001 is always reported on unions offering to generate factory methods, as it is not technically possible to implement a code fix for SDU0009 (See issue).
Product | Versions 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. |
.NET Core | netcoreapp1.0 was computed. netcoreapp1.1 was computed. netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard1.0 is compatible. netstandard1.1 was computed. netstandard1.2 was computed. netstandard1.3 was computed. netstandard1.4 was computed. netstandard1.5 was computed. netstandard1.6 was computed. netstandard2.0 was computed. netstandard2.1 was computed. |
.NET Framework | net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen30 was computed. tizen40 was computed. tizen60 was computed. |
Universal Windows Platform | uap was computed. uap10.0 was computed. |
Windows Phone | wp8 was computed. wp81 was computed. wpa81 was computed. |
Windows Store | netcore was computed. netcore45 was computed. netcore451 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
This package has no dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Sundew.DiscriminatedUnions:
Package | Downloads |
---|---|
Sundew.Base.Collections.Linq
Extensions for collections types. |
GitHub repositories
This package is not used by any popular GitHub repositories.
2.0 - Support matching unions in referenced projects, marker attributes included as source rather than library reference.
1.0 - Initial version