Dusharp 0.2.0
See the version list below for details.
dotnet add package Dusharp --version 0.2.0
NuGet\Install-Package Dusharp -Version 0.2.0
<PackageReference Include="Dusharp" Version="0.2.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="Dusharp" Version="0.2.0" />
<PackageReference Include="Dusharp"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add Dusharp --version 0.2.0
#r "nuget: Dusharp, 0.2.0"
#:package Dusharp@0.2.0
#addin nuget:?package=Dusharp&version=0.2.0
#tool nuget:?package=Dusharp&version=0.2.0
Dusharp
Dusharp is a C# source generator for creating discriminated unions. This library allows you to define union types with ease, using attributes and partial methods. It is inspired by functional languages but built for C# developers.
Features
- ✅ Create unions: Define discriminated unions using attributes.
- ✅ Match method: Pattern match on union cases in a type-safe way.
- ✅ Equality: Automatic equality comparison for unions.
- ❌ Pretty print: Using overloaded
ToString()(coming soon). - ❌ JSON serialization/deserialization: Support for unions with
System.Text.Json(coming soon). - ❌ Struct unions: With efficient memory layout for unions as structs (coming soon).
Installation
Dusharp is available as a NuGet package. You can install it using the NuGet package manager:
dotnet add package Dusharp
Usage
Dusharp uses attributes to generate discriminated unions and case methods. Here's how to get started:
1. Define a Union
To define a union, annotate a class with the [Dusharp.UnionAttribute] attribute.
using Dusharp;
[Union]
public partial class Shape { }
2. Define Union Cases
Define union cases by creating public static partial methods and marking them with the [Dusharp.UnionCaseAttribute] attribute. The method body will be automatically generated.
using Dusharp;
[Union]
public partial class Shape
{
[UnionCase]
public static partial Shape Circle(double radius);
[UnionCase]
public static partial Shape Rectangle(double width, double height);
}
3. Match on Union
You can easily perform pattern matching on a union using the Match method. The source generator will create the Match method based on the defined union cases.
Shape shape = Shape.Circle(5.0);
string result = shape.Match(
radius => $"Circle with radius {radius}",
(width, height) => $"Rectangle with width {width} and height {height}"
);
Console.WriteLine(result); // Output: Circle with radius 5.0
4. Compare Unions
Union cases can be compared for equality using the auto-generated equality methods. This allows for checking if two unions are the same.
Shape shape1 = Shape.Circle(5.0);
Shape shape2 = Shape.Circle(5.0);
bool areEqual = shape1.Equals(shape2); // true
Upcoming Features
- Pretty printing: A default ToString() implementation for unions that provides a readable representation of union cases.
- JSON serialization/deserialization: Support for JSON (de)serialization via System.Text.Json.
- Struct unions: More efficient unions using structs with effective data layout.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Dusharp:
| Package | Downloads |
|---|---|
|
Dusharp.Json
System.Text.Json support for Dusharp. |
|
|
Dusharp.Newtonsoft
Newtonsoft.Json support for Dusharp. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.7.2 | 1,054 | 11/2/2025 |
| 0.7.1 | 313 | 10/29/2025 |
| 0.7.0 | 416 | 4/25/2025 |
| 0.6.2 | 240 | 1/10/2025 |
| 0.6.1 | 189 | 1/9/2025 |
| 0.6.0 | 219 | 1/7/2025 |
| 0.6.0-rc.1 | 139 | 1/7/2025 |
| 0.5.3 | 264 | 10/12/2024 |
| 0.5.2 | 217 | 10/1/2024 |
| 0.5.1 | 190 | 9/30/2024 |
| 0.5.0 | 231 | 9/29/2024 |
| 0.4.0 | 284 | 9/14/2024 |
| 0.3.0 | 213 | 9/13/2024 |
| 0.2.0 | 246 | 9/12/2024 |