-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from ignatandrei/300-dusharp
dusharp
- Loading branch information
Showing
43 changed files
with
1,481 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
//if (x1.res == 2) | ||
//{ | ||
// Console.WriteLine("asdasd"); | ||
// return; | ||
// return; | ||
//} | ||
try | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
|
||
<h1>RSCG nr 160 : Dusharp</h1> | ||
|
||
<h2>Info</h2> | ||
Nuget : <a href="https://www.nuget.org/packages/Dusharp/" target="_blank">https://www.nuget.org/packages/Dusharp/</a> | ||
|
||
<p>You can find more details at : <a href="https://github.com/kolebynov/Dusharp" target="_blank"> https://github.com/kolebynov/Dusharp</a></p> | ||
|
||
<p>Author :Vitali</p> | ||
|
||
<p>Source : <a href="https://github.com/kolebynov/Dusharp" target="_blank">https://github.com/kolebynov/Dusharp</a> </p> | ||
|
||
<h2>About</h2> | ||
|
||
Generate tagged union | ||
|
||
<h2> | ||
How to use | ||
</h2> | ||
<h3> | ||
Add reference to the <a href="https://www.nuget.org/packages/Dusharp/" target="_blank">Dusharp</a> in the csproj | ||
</h3> | ||
<img src="images/Dusharp/UnionTypesDemo.csproj.png" width="580" height="580" /> | ||
|
||
<h3>This was for me the <b>starting</b> code</h3> | ||
|
||
<br /> | ||
I have <b>coded</b> the file Program.cs | ||
<br /> | ||
<img src="images/Dusharp/csFiles/Program.cs.png" width="580" height="580" /> | ||
<hr /> | ||
|
||
<br /> | ||
I have <b>coded</b> the file ResultSave.cs | ||
<br /> | ||
<img src="images/Dusharp/csFiles/ResultSave.cs.png" width="580" height="580" /> | ||
<hr /> | ||
|
||
<br /> | ||
I have <b>coded</b> the file SaveToDatabase.cs | ||
<br /> | ||
<img src="images/Dusharp/csFiles/SaveToDatabase.cs.png" width="580" height="580" /> | ||
<hr /> | ||
<h3>And here are the <i>generated</i> files</h3> | ||
|
||
<br /> | ||
The file <i>generated</i> is Dusharp.EmbeddedCode.ExceptionUtils.cs | ||
<br /> | ||
<img src="images/Dusharp/generated/Dusharp.EmbeddedCode.ExceptionUtils.cs.png" width="580" height="580" /> | ||
|
||
<br /> | ||
The file <i>generated</i> is Dusharp.EmbeddedCode.UnionAttribute.cs | ||
<br /> | ||
<img src="images/Dusharp/generated/Dusharp.EmbeddedCode.UnionAttribute.cs.png" width="580" height="580" /> | ||
|
||
<br /> | ||
The file <i>generated</i> is Dusharp.EmbeddedCode.UnionCaseAttribute.cs | ||
<br /> | ||
<img src="images/Dusharp/generated/Dusharp.EmbeddedCode.UnionCaseAttribute.cs.png" width="580" height="580" /> | ||
|
||
<br /> | ||
The file <i>generated</i> is UnionTypesDemo.ResultSave.Dusharp.g.cs | ||
<br /> | ||
<img src="images/Dusharp/generated/UnionTypesDemo.ResultSave.Dusharp.g.cs.png" width="580" height="580" /> | ||
|
||
<br /> | ||
The file <i>generated</i> is UnionTypesDemo.ResultSave.union.g.cs | ||
<br /> | ||
<img src="images/Dusharp/generated/UnionTypesDemo.ResultSave.union.g.cs.png" width="580" height="580" /> | ||
|
||
<p> | ||
You can download the code and this page as pdf from | ||
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/Dusharp'> | ||
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Dusharp | ||
</a> | ||
</p> | ||
|
||
|
||
<p> | ||
You can see the whole list at | ||
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG'> | ||
https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG | ||
</a> | ||
</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"generator":{ | ||
"name":"Dusharp", | ||
"nuget":[ | ||
"https://www.nuget.org/packages/Dusharp/" | ||
], | ||
"link":"https://github.com/kolebynov/Dusharp", | ||
"author":"Vitali", | ||
"source":"https://github.com/kolebynov/Dusharp" | ||
}, | ||
"data":{ | ||
"goodFor":["Generate tagged union"], | ||
"csprojDemo":"UnionTypesDemo.csproj", | ||
"csFiles":["Program.cs","ResultSave.cs","SaveToDatabase.cs"], | ||
"excludeDirectoryGenerated":[""], | ||
"includeAdditionalFiles":[""] | ||
}, | ||
"links":{ | ||
"blog":"", | ||
"video":"" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Dusharp is a C# source generator for creating discriminated unions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Dusharp | ||
|
||
[![NuGet](https://img.shields.io/nuget/v/Dusharp)](https://www.nuget.org/packages/Dusharp/) | ||
|
||
**Dusharp** is a C# source generator library 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. | ||
- ✅ **Generics**: Generics support for union types. | ||
- ✅ **Pretty print**: Using overloaded `ToString()`. | ||
- ❌ **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: | ||
|
||
```bash | ||
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. | ||
|
||
```csharp | ||
using Dusharp; | ||
|
||
[Union] | ||
public partial class Shape<T> | ||
where T : struct, INumber<T> | ||
{ | ||
} | ||
``` | ||
|
||
### 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. | ||
|
||
```csharp | ||
using Dusharp; | ||
|
||
[Union] | ||
public partial class Shape<T> | ||
where T : struct, INumber<T> | ||
{ | ||
[UnionCase] | ||
public static partial Shape<T> Circle(T radius); | ||
|
||
[UnionCase] | ||
public static partial Shape<T> Rectangle(T width, T 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. | ||
|
||
```csharp | ||
Shape<double> shape = Shape<double>.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. | ||
|
||
```csharp | ||
Shape<double> shape1 = Shape<double>.Circle(5.0); | ||
Shape<double> shape2 = Shape<double>.Circle(5.0); | ||
|
||
Console.WriteLine(shape1.Equals(shape2)); // True | ||
Console.WriteLine(shape1 == shape2); // True | ||
``` | ||
|
||
## Upcoming Features | ||
- **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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
|
||
{ | ||
"$schema": "https://aka.ms/codetour-schema", | ||
"title": "Dusharp", | ||
"steps": | ||
[ | ||
{ | ||
"file": "UnionTypesDemo/UnionTypesDemo.csproj", | ||
"description": "First, we add Nuget [Dusharp](https://www.nuget.org/packages/Dusharp/) in csproj ", | ||
"pattern": "Dusharp" | ||
} | ||
|
||
,{ | ||
"file": "UnionTypesDemo/SaveToDatabase.cs", | ||
"description": "File SaveToDatabase.cs ", | ||
"pattern": "this is the code" | ||
} | ||
|
||
,{ | ||
"file": "UnionTypesDemo/ResultSave.cs", | ||
"description": "File ResultSave.cs ", | ||
"pattern": "this is the code" | ||
} | ||
|
||
,{ | ||
"file": "UnionTypesDemo/Program.cs", | ||
"description": "File Program.cs \r\n>> dotnet run --project UnionTypesDemo/UnionTypesDemo.csproj ", | ||
"pattern": "this is the code" | ||
} | ||
|
||
|
||
,{ | ||
"file": "UnionTypesDemo/obj/GX/Sera.TaggedUnion.Analyzers/Sera.TaggedUnion.Analyzers.Generators.UnionGenerator/UnionTypesDemo.ResultSave.union.g.cs", | ||
"description": "Generated File 5 from 5 : UnionTypesDemo.ResultSave.union.g.cs ", | ||
"line": 1 | ||
} | ||
|
||
,{ | ||
"file": "UnionTypesDemo/obj/GX/Dusharp/Dusharp.UnionSourceGenerator/UnionTypesDemo.ResultSave.Dusharp.g.cs", | ||
"description": "Generated File 4 from 5 : UnionTypesDemo.ResultSave.Dusharp.g.cs ", | ||
"line": 1 | ||
} | ||
|
||
,{ | ||
"file": "UnionTypesDemo/obj/GX/Dusharp/Dusharp.UnionSourceGenerator/Dusharp.EmbeddedCode.UnionCaseAttribute.cs", | ||
"description": "Generated File 3 from 5 : Dusharp.EmbeddedCode.UnionCaseAttribute.cs ", | ||
"line": 1 | ||
} | ||
|
||
,{ | ||
"file": "UnionTypesDemo/obj/GX/Dusharp/Dusharp.UnionSourceGenerator/Dusharp.EmbeddedCode.UnionAttribute.cs", | ||
"description": "Generated File 2 from 5 : Dusharp.EmbeddedCode.UnionAttribute.cs ", | ||
"line": 1 | ||
} | ||
|
||
,{ | ||
"file": "UnionTypesDemo/obj/GX/Dusharp/Dusharp.UnionSourceGenerator/Dusharp.EmbeddedCode.ExceptionUtils.cs", | ||
"description": "Generated File 1 from 5 : Dusharp.EmbeddedCode.ExceptionUtils.cs ", | ||
"line": 1 | ||
} | ||
|
||
], | ||
|
||
"ref": "main" | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 17 | ||
VisualStudioVersion = 17.7.34221.43 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnionTypesDemo", "UnionTypesDemo\UnionTypesDemo.csproj", "{B3F52D30-D0FB-4DF2-B494-6B903B16613D}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{B3F52D30-D0FB-4DF2-B494-6B903B16613D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{B3F52D30-D0FB-4DF2-B494-6B903B16613D}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{B3F52D30-D0FB-4DF2-B494-6B903B16613D}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{B3F52D30-D0FB-4DF2-B494-6B903B16613D}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {7C63CD9F-20A2-4EAB-9766-127A7184E23B} | ||
EndGlobalSection | ||
EndGlobal |
Oops, something went wrong.