UnionTypes.Toolkit.Generator
2.3.0
dotnet add package UnionTypes.Toolkit.Generator --version 2.3.0
NuGet\Install-Package UnionTypes.Toolkit.Generator -Version 2.3.0
<PackageReference Include="UnionTypes.Toolkit.Generator" Version="2.3.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="UnionTypes.Toolkit.Generator" Version="2.3.0" />
<PackageReference Include="UnionTypes.Toolkit.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add UnionTypes.Toolkit.Generator --version 2.3.0
#r "nuget: UnionTypes.Toolkit.Generator, 2.3.0"
#:package UnionTypes.Toolkit.Generator@2.3.0
#addin nuget:?package=UnionTypes.Toolkit.Generator&version=2.3.0
#tool nuget:?package=UnionTypes.Toolkit.Generator&version=2.3.0
UnionTypes.Toolkit.Generator
A C# source generator package for generating custom union types compatible with the C# union types feature.
It currently implements a source generator that generates non-boxing unions with a variety of customization options to help you generate the best storage layout for your union's case types.
The generator is purely standalone; the generated union source does not depend on other libraries at runtime to function.
It may include additional generators in the future.
Declaring a Non-Boxing Custom Union Type
Declare a partial struct type with @union in its leading comments and a partial constructor for each case type for the generator to implement.
The generator will layout the contents of the custom union so that the fields storing the different case types are overlapped with each other using the same memory space, if possible, or at least consume less space than simply having separate fields for each case type. No case value is boxed.
// @union
public partial struct MyUnion
{
public partial MyUnion(int value);
public partial MyUnion(float value);
public partial MyUnion(string value);
public partial MyUnion(IManifest value);
public partial MyUnion(Coordinate value);
public partial MyUnion(Address value);
}
record struct Coordinate(float Longitude, float Latitude);
record struct Address(int Id, string Name);
interface IManifest { ... }
In this example, there will be a single field storing a struct that contains enough space to store either an int, float, Coordinate or the address Id and a sparate object field used to store either a string, IManifest or the address Name.
Discriminated Unions
To declare a union that acts like a more traditional discriminated union, declare the partial struct with nested type declarations (no constructors) and add the @factory property in the comments.
// @union @factory
public partial struct MyUnion
{
public record struct Coordinate(float Longitude, float Latitude);
public record struct Address(int Id, string Name);
public record struct Unknown;
}
var u = MyUnion.Coordinate(101f, 223f);
...
var _ = u switch
{
MyUnion.Coordinate(var longitude, var latitude) => ...,
MyUnion.Address a => ...,
MyUnion.Unknown => ...;
};
Documentation
For more detailed information on using the generator and ways to customize the generated union types, Click Here
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.