Funcky.DiscriminatedUnion
0.1.0
See the version list below for details.
dotnet add package Funcky.DiscriminatedUnion --version 0.1.0
NuGet\Install-Package Funcky.DiscriminatedUnion -Version 0.1.0
<PackageReference Include="Funcky.DiscriminatedUnion" Version="0.1.0" />
paket add Funcky.DiscriminatedUnion --version 0.1.0
#r "nuget: Funcky.DiscriminatedUnion, 0.1.0"
// Install Funcky.DiscriminatedUnion as a Cake Addin #addin nuget:?package=Funcky.DiscriminatedUnion&version=0.1.0 // Install Funcky.DiscriminatedUnion as a Cake Tool #tool nuget:?package=Funcky.DiscriminatedUnion&version=0.1.0
Funcky Discriminated Unions
A source generator that generates Match
methods for all your discriminated unions needs. ✨
Usage
Apply the [DiscriminatedUnion]
to an abstract class (or record) with nested types representing the variants.
Example
using Funcky;
var result = Result<int>.Ok(42);
var resultOrFallback = result.Match(ok: ok => ok.Value, error: _ => 0);
[DiscriminatedUnion]
public abstract partial record Result<T>
where T : notnull
{
public sealed partial record Ok(T Value) : Result<T>;
public sealed partial record Error(Exception Exception) : Result<T>;
}
Minimum Required Versions
- Visual Studio 2022
- Roslyn 4.0.0
- .NET 6
Settings
The attribute allows configuration of some aspects of source generation.
NonExhaustive
The auto-generated Match
and Switch
methods are public by default.
When NonExhaustive
is set to true
, these methods are generated with internal
visibility instead.
MatchResultTypeName
The auto-generated Match
method uses a generic type for the result. This type is named TResult
by default.
This can cause conflict with generic types on the discriminated union itself. Use MatchResultTypeName
to set a custom name for this type.
using Funcky;
[DiscriminatedUnion(MatchResultTypeName = "TMatchResult")]
public abstract partial record Result<TResult> { ... }
// Generated code
partial record Result<TResult>
{
public abstract TMatchResult Match<TMatchResult>(...);
...
}
Flatten
The auto-generated Match
and Switch
methods only accept one level of inheritance by default.
Set Flatten
to true
to include arbitrarily deep inherited types in these methods.
using Funcky;
SyntaxNode node = ...;
var nodeAsString = node.Match(
keyword: keyword => keyword.Value,
integer: integer => integer.Value.ToString(),
double: @double => @double.Value.ToString());
[DiscriminatedUnion(Flatten = true)]
public abstract partial record SyntaxNode
{
public sealed partial record Keyword(string Value) : SyntaxNode;
public abstract partial record Literal : SyntaxNode;
public abstract partial record Number : Literal;
public sealed partial record Integer(int Value) : Number;
public sealed partial record Double(double Value) : Number;
}
Learn more about Target Frameworks and .NET Standard.
This package has 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.