Dartk.CSharp.DiscriminatedUnions 0.1.1

Prefix Reserved
dotnet add package Dartk.CSharp.DiscriminatedUnions --version 0.1.1                
NuGet\Install-Package Dartk.CSharp.DiscriminatedUnions -Version 0.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dartk.CSharp.DiscriminatedUnions" Version="0.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dartk.CSharp.DiscriminatedUnions --version 0.1.1                
#r "nuget: Dartk.CSharp.DiscriminatedUnions, 0.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dartk.CSharp.DiscriminatedUnions as a Cake Addin
#addin nuget:?package=Dartk.CSharp.DiscriminatedUnions&version=0.1.1

// Install Dartk.CSharp.DiscriminatedUnions as a Cake Tool
#tool nuget:?package=Dartk.CSharp.DiscriminatedUnions&version=0.1.1                

About

Dartk.CSharp.DiscriminatedUnions is a discriminated union source generator for C#.

Discriminated unions represent values that can be one of a number of cases. Each case has a unique name and can store values of different types, as opposed to the standard enum type, that can only be of an integral numeric type.

More information is available at the dartk/CSharp.DiscriminatedUnions github page.

Installation

Generated code does not depend on the package at runtime. Therefore, it is safe to set the option PrivateAssets="all" to avoid propagating the dependency on the package:

<ItemGroup>
    <PackageReference Include="Dartk.CSharp.DiscriminatedUnions" Version="0.1.1" PrivateAssets="all" />
</ItemGroup>

How to Use

Declaring a Discriminated Union
[DiscriminatedUnion]
partial class Shape
{
    [Case] public static partial Shape Dot();
    [Case] public static partial Shape Circle(double radius);
    [Case] public static partial Shape Rectangle(double width, double length);
}
Creating an Instance
var dot = Shape.Dot();
var circle = Shape.Circle(5.0);
var rectangle = Shape.Rectangle(2.0, 4.0);
Case Matching
double Area(Shape shape) => shape.Switch(
    Dot: () => 0.0,
    Circle: radius => Math.PI * radius * radius,
    Rectangle: (width, length) => width * length
);

bool IsCircle(Shape shape) => shape.Switch(
    Circle: _ => true,
    Default: _ => false
);
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • 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.

Version Downloads Last updated
0.1.1 327 3/14/2023
0.1.0 278 2/14/2023