N.SourceGenerators.UnionTypes
0.5.0
See the version list below for details.
dotnet add package N.SourceGenerators.UnionTypes --version 0.5.0
NuGet\Install-Package N.SourceGenerators.UnionTypes -Version 0.5.0
<PackageReference Include="N.SourceGenerators.UnionTypes" Version="0.5.0" />
paket add N.SourceGenerators.UnionTypes --version 0.5.0
#r "nuget: N.SourceGenerators.UnionTypes, 0.5.0"
// Install N.SourceGenerators.UnionTypes as a Cake Addin #addin nuget:?package=N.SourceGenerators.UnionTypes&version=0.5.0 // Install N.SourceGenerators.UnionTypes as a Cake Tool #tool nuget:?package=N.SourceGenerators.UnionTypes&version=0.5.0
N.SourceGenerators.UnionTypes
Discriminated union type source generator
Motivation
C# doesn't support discriminated unions yet. This source generator helps automate writing union types.
Using
Add package reference to N.SourceGenerators.UnionTypes
dotnet add package N.SourceGenerators.UnionTypes
Create a partial class that will be used as a union type
public partial class FooResult
{
}
Add types you want to use in a discriminated union
public record Success(int Value);
public record ValidationError(string Message);
public record NotFoundError;
public partial class FooResult
{
}
Add N.SourceGenerators.UnionTypes.UnionTypeAttribute
to a union type.
using N.SourceGenerators.UnionTypes;
public record Success(int Value);
public record ValidationError(string Message);
public record NotFoundError;
[UnionType(typeof(Success))]
[UnionType(typeof(ValidationError))]
[UnionType(typeof(NotFoundError))]
public partial class FooResult
{
}
Examples
All examples can be found in examples project
Implicit conversion
public FooResult ImplicitReturn()
{
// you can return any union type variation without creating FooResult
return new NotFoundError();
}
Explicit conversion
public ValidationError ExplicitCast(FooResult result)
{
return (ValidationError)result;
}
Match and MatchAsync methods force you to handle all possible variations
public IActionResult MatchMethod(FooResult result)
{
return result.Match<IActionResult>(
success => new OkResult(),
validationError => new BadRequestResult(),
notFoundError => new NotFoundResult()
);
}
public async Task<IActionResult> MatchAsyncMethod(FooResult result, CancellationToken cancellationToken)
{
return await result.MatchAsync<IActionResult>(
static async (success, ct) =>
{
await SomeWork(ct);
return new OkResult();
}, static async (validationError, ct) =>
{
await SomeWork(ct);
return new BadRequestResult();
}, static async (notFoundError, ct) =>
{
await SomeWork(ct);
return new NotFoundResult();
}, cancellationToken);
static Task SomeWork(CancellationToken ct)
{
return Task.Delay(100, ct);
}
}
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on N.SourceGenerators.UnionTypes:
Package | Downloads |
---|---|
Zomp.SyncMethodGenerator
Generates synchronized method from async method |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.28.0 | 4,624 | 5/2/2024 |
0.28.0-rc.61 | 30 | 5/2/2024 |
0.28.0-rc.60 | 23 | 5/2/2024 |
0.27.0 | 1,493 | 3/19/2024 |
0.27.0-rc.58 | 59 | 3/19/2024 |
0.27.0-rc.57 | 77 | 3/13/2024 |
0.26.0 | 35,324 | 10/26/2023 |
0.26.0-rc.55 | 71 | 11/16/2023 |
0.26.0-rc.53 | 77 | 10/30/2023 |
0.26.0-rc.52 | 75 | 10/30/2023 |
0.26.0-rc.51 | 79 | 10/30/2023 |
0.26.0-rc.50 | 69 | 10/30/2023 |
0.26.0-rc.48 | 76 | 10/26/2023 |
0.25.3 | 367 | 10/18/2023 |
0.25.3-rc.41 | 78 | 10/18/2023 |
0.25.1-rc.40 | 332 | 10/17/2023 |
0.25.0-rc.39 | 90 | 10/17/2023 |
0.24.0 | 1,024 | 10/17/2023 |
0.24.0-rc.37 | 126 | 10/16/2023 |
0.23.0 | 712 | 10/3/2023 |
0.23.0-rc.35 | 302 | 10/2/2023 |
0.22.0 | 319 | 10/2/2023 |
0.22.0-rc.33 | 77 | 10/2/2023 |
0.21.0 | 282 | 10/2/2023 |
0.21.0-rc.31 | 79 | 10/2/2023 |
0.21.0-rc.30 | 86 | 9/28/2023 |
0.20.0-rc.29 | 908 | 9/6/2023 |
0.20.0-rc.28 | 60 | 9/6/2023 |
0.19.0 | 673 | 8/3/2023 |
0.18.0 | 523 | 2/5/2023 |
0.17.0 | 362 | 2/2/2023 |
0.16.0 | 332 | 1/30/2023 |
0.15.0 | 364 | 1/29/2023 |
0.14.0 | 356 | 1/21/2023 |
0.13.0 | 372 | 1/20/2023 |
0.12.0 | 390 | 1/20/2023 |
0.11.0 | 375 | 1/18/2023 |
0.10.0 | 377 | 1/18/2023 |
0.9.0 | 372 | 1/17/2023 |
0.8.0 | 353 | 1/17/2023 |
0.7.0 | 336 | 1/17/2023 |
0.6.0 | 352 | 1/15/2023 |
0.5.1 | 362 | 1/15/2023 |
0.5.0 | 383 | 1/15/2023 |
0.4.0 | 391 | 1/15/2023 |
0.3.0 | 410 | 1/15/2023 |
0.2.0 | 398 | 1/15/2023 |
0.1.0 | 464 | 1/15/2023 |