EasyUnions.SourceGenerator 0.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package EasyUnions.SourceGenerator --version 0.0.1
NuGet\Install-Package EasyUnions.SourceGenerator -Version 0.0.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="EasyUnions.SourceGenerator" Version="0.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EasyUnions.SourceGenerator --version 0.0.1
#r "nuget: EasyUnions.SourceGenerator, 0.0.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 EasyUnions.SourceGenerator as a Cake Addin
#addin nuget:?package=EasyUnions.SourceGenerator&version=0.0.1

// Install EasyUnions.SourceGenerator as a Cake Tool
#tool nuget:?package=EasyUnions.SourceGenerator&version=0.0.1

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

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in 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.0.8 175 9/8/2023
0.0.7 144 9/7/2023
0.0.6 204 8/20/2023
0.0.5 165 8/20/2023
0.0.3 226 8/20/2023
0.0.1 108 8/20/2023

First version, currently with basic support for discriminated unions