EasyUnions.SourceGenerator
0.0.8
dotnet add package EasyUnions.SourceGenerator --version 0.0.8
NuGet\Install-Package EasyUnions.SourceGenerator -Version 0.0.8
<PackageReference Include="EasyUnions.SourceGenerator" Version="0.0.8" />
paket add EasyUnions.SourceGenerator --version 0.0.8
#r "nuget: EasyUnions.SourceGenerator, 0.0.8"
// Install EasyUnions.SourceGenerator as a Cake Addin #addin nuget:?package=EasyUnions.SourceGenerator&version=0.0.8 // Install EasyUnions.SourceGenerator as a Cake Tool #tool nuget:?package=EasyUnions.SourceGenerator&version=0.0.8
Easy Unions
Use it for making easy unions into your code!
Usage
To use, it is very simple. Suppose you have two classes, Tree and Node, and you want to represent both of them in a single union type. You can do this:
[Maybe("TreeOrNode", 0)]
public class Tree
{
public void Dump() {...}
}
[Maybe("TreeOrNode", 1)]
public class Node
{
public void Dump() {...}
}
By using that attribute, you are assigning both of them as 'variants' of a type that will be called 'TreeOrNode'. And so, the first argument is the name of the union (to use it), and the second is the discriminant for this type inside the union (and thus, Tree will be represented as 0 and Node as 1). You can decorate, currently, both classes and structs with this attribute.
Now, you can use it like this:
TreeOrNode treeOrNode = new Tree();
treeOrNode.Match(tree => tree.Dump(), node => node.Dump());
In this example, it will match the first lambda as the union represents a tree, if it uld be a node, it would match the second lambda. There is also a variant of 'Match' wich allow the return of some value at the end of the function. The generated unions assign automatically by implicit casting from the represented types, but you can also use the static generated methods to create the union:
var treeOrNode = TreeOrNode.From(new Tree());
...
If you have a union with many represented types, and know which of them to handle in a specific case (and also avoid the use of Match), you can use the 'Try...' methods:
var treeOrNode = TreeOrNode.From(new Tree());
if (treeOrNode.TryTree(out Tree tree))
tree.Dump();
In this example, the 'TryTree' is automatically generated by the source generator (and there is also a TryNode for example), and it will return true if the union is a tree, and false otherwise. The 'out' will be assigned with the represented tree.
Future plans
I plan also to to support cooler things like to access the methods, fields and properties that are common to the unions without needing to use Match or Try on them. I also plan to support more types of unions, like 'both' (where you can have both types at the same time), and also support types that are not classes or structs nor are user-defined (like int, float, etc).
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.
You can now generate multiple unions with the same types.