TA.SourceGenerators.EnumStruct 1.1.0

dotnet add package TA.SourceGenerators.EnumStruct --version 1.1.0
NuGet\Install-Package TA.SourceGenerators.EnumStruct -Version 1.1.0
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="TA.SourceGenerators.EnumStruct" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TA.SourceGenerators.EnumStruct --version 1.1.0
#r "nuget: TA.SourceGenerators.EnumStruct, 1.1.0"
#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 TA.SourceGenerators.EnumStruct as a Cake Addin
#addin nuget:?package=TA.SourceGenerators.EnumStruct&version=1.1.0

// Install TA.SourceGenerators.EnumStruct as a Cake Tool
#tool nuget:?package=TA.SourceGenerators.EnumStruct&version=1.1.0

This package contains a source generator which allows the creation of "enum struct" discriminated unions.

public interface IHasGenericId { }
public class SomeEntity : IHasGenericId { }

#pragma warning disable CS0282 // There is no defined ordering between fields in multiple declarations of partial struct
[EnumStruct]
readonly partial struct SearchResult
{
	public readonly partial struct NotFound
	{
	}
	public readonly partial struct ArgumentNull
	{
		public int ErrorCode { get; }
	}
	public readonly partial struct Found
	{
		public IHasGenericId Entity { get; }
	}
	public readonly partial struct FoundMoreThanOne
	{
		public IHasGenericId[] Items { get; }
	}

	public string CommonProp { get; }
}
#pragma warning restore CS0282 // There is no defined ordering between fields in multiple declarations of partial struct

The CommonProp is "common state" for all options, and additional state for each option is specified in each corresponding nested struct.

This corresponds to the following imaginary enum struct syntax of the future:

enum struct SearchResult(string CommonProp)
{
	NotFound,
	ArgumentNull(int ErrorCode),
	Found(IHasGenericId Entity),
	FoundMoreThanOne(IHasGenericId[] Items),
}

Usage:

SearchResult Search()
{
	return new SearchResult2.Found("found", new SomeEntity());
}


var sr = Search();
if(sr.FoundOption is { } found) // "...Option" properties return nullable option value
{
	Console.WriteLine(found.Entity);
	Console.WriteLine(found.CommonProp);	
}

// switch can be written like this: (exhaustiveness is not enforced unfortunately)
switch (res)
{
	case { FoundOption: { } found }:
		break;
	case { NotFoundOption: { } }:
		break;
}

To quickly filter a collection, the following LINQ extension method can be used:

public static IEnumerable<TResult> WhereNotNullStruct<T, TResult>(this IEnumerable<T> This, Func<T, TResult?> selector)
		where TResult : struct
{
	foreach (var item in This)
	{
		var res = selector(item);
		if (res != null)
			yield return res.Value;
	}
}

SearchResult[] list = SearchMany();
var onlyFound = list.WhereNotNullStruct(x => x.FoundOption).ToList();

The above generates the following code:

partial struct SearchResult
{
	partial struct NotFound
	{
		[System.Obsolete("Do not create empty instances and do not use default either", true)]
		public NotFound() { throw new System.NotSupportedException(); }
		public NotFound(string commonProp)
		{
			this.CommonProp = commonProp;
		}
		public string CommonProp { get; }
	}
	partial struct ArgumentNull
	{
		[System.Obsolete("Do not create empty instances and do not use default either", true)]
		public ArgumentNull() { throw new System.NotSupportedException(); }
		public ArgumentNull(string commonProp, int errorCode)
		{
			this.CommonProp = commonProp;
			this.ErrorCode = errorCode;
		}
		public string CommonProp { get; }
	}
	partial struct Found
	{
		[System.Obsolete("Do not create empty instances and do not use default either", true)]
		public Found() { throw new System.NotSupportedException(); }
		public Found(string commonProp, EnumStructTest.IHasGenericId entity)
		{
			this.CommonProp = commonProp;
			this.Entity = entity;
		}
		public string CommonProp { get; }
	}
	partial struct FoundMoreThanOne
	{
		[System.Obsolete("Do not create empty instances and do not use default either", true)]
		public FoundMoreThanOne() { throw new System.NotSupportedException(); }
		public FoundMoreThanOne(string commonProp, EnumStructTest.IHasGenericId[] items)
		{
			this.CommonProp = commonProp;
			this.Items = items;
		}
		public string CommonProp { get; }
	}

	public static implicit operator SearchResult(NotFound notFound) => new(1, notFound.CommonProp, default, default);
	public static implicit operator SearchResult(ArgumentNull argumentNull) => new(2, argumentNull.CommonProp, argumentNull.ErrorCode, default);
	public static implicit operator SearchResult(Found found) => new(3, found.CommonProp, default, found.Entity);
	public static implicit operator SearchResult(FoundMoreThanOne foundMoreThanOne) => new(4, foundMoreThanOne.CommonProp, default, foundMoreThanOne.Items);
	
	[System.Obsolete("Do not create empty instances, use default instead", true)]
	public SearchResult() { throw new System.NotSupportedException(); }
	private SearchResult(int tag, string commonProp, int int32Field0, object? objectField0)
	{
		this._tag = tag;
		this.CommonProp = commonProp;
		this._int32Field0 = int32Field0;
		this._objectField0 = objectField0;
	}

	public NotFound? NotFoundOption
	{
		get
		{
			if (this._tag == 1)
			{
				return new NotFound(this.CommonProp);
			}
			return null;
		}
	}
	public ArgumentNull? ArgumentNullOption
	{
		get
		{
			if (this._tag == 2)
			{
				return new ArgumentNull(this.CommonProp, this._int32Field0);
			}
			return null;
		}
	}
	public Found? FoundOption
	{
		get
		{
			if (this._tag == 3)
			{
				return new Found(this.CommonProp, System.Runtime.CompilerServices.Unsafe.As<EnumStructTest.IHasGenericId>(this._objectField0!));
			}
			return null;
		}
	}
	public FoundMoreThanOne? FoundMoreThanOneOption
	{
		get
		{
			if (this._tag == 4)
			{
				return new FoundMoreThanOne(this.CommonProp, System.Runtime.CompilerServices.Unsafe.As<EnumStructTest.IHasGenericId[]>(this._objectField0!));
			}
			return null;
		}
	}

	readonly int _tag;
	readonly int _int32Field0;
	readonly object? _objectField0;
}
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
1.1.0 92 2/17/2024
1.0.0 111 2/11/2024