Emik.SourceGenerators.Choices 1.4.0

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

// Install Emik.SourceGenerators.Choices as a Cake Tool
#tool nuget:?package=Emik.SourceGenerators.Choices&version=1.4.0                

Emik.SourceGenerators.Choices

NuGet package License

Annotate [Choice] to transform your type into a highly performant and flexible disjoint union.

Massive thanks to hikarin522's ValueVariant for being the inspiration to this source generator.

This source generator assumes the project it is generating for to be at least C# version 6.0, or 8.0 if the variants include pointer types or ref-like types.

This project has a dependency to Emik.Morsels, if you are building this project, refer to its README first.



Examples

Usages
Result<string, int> result = "Success"; // Implicit conversion to `Ok`.
Encoding encoding = default; // Defaults to the first variant, which is `Utf8`, with the default value of `Span<byte>`.
Result result = new(ok: 1m); // Constructor for `Ok`.
Option<string> option = Option.OfSome("Hello"); // Factory method to `Some`.

if (result.IsOk) // Checks for whether the union is `Ok`.
    Console.WriteLine(result.Ok.GetHashCode()); // `Ok` is flagged as non-null to the nullable analyzer.

var length = encoding.Length; // Since both variants have a `Length` property, you can access it from the union directly.
encoding.Clear(); // Since both variants have a `Clear` method, you can invoke it from the union directly.
var value = result.GetUnderlyingValue(); // Returns `ISerializable` since both variants implement or are `ISerializable`.
var isEmpty = option.Map(x => x.Length is 0, () => true); // Exhaustive match.
var maybe = (string?)option; // Explicit cast back to `string?`.
Declarations
using Emik;

[Choice]
readonly partial record struct Result<TOk, TErr>(TOk? ok, TErr? err);

[Choice.Utf8<Span<byte>>.Utf16<Span<char>>]
ref partial struct Encoding;

[Choice(typeof((ISerializable Ok, Exception Err)))]
sealed partial class Result;

[Choice]
readonly partial struct Option<T>
{
    readonly T _some;

    ValueTuple None => default;
}

For more examples, take a look at the playground or the unit tests, and the expected output.

Contribute

Issues and pull requests are welcome to help this repository be the best it can be.

License

This repository falls under the MPL-2 license.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has 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.4.0 147 9/30/2024
1.3.3 98 9/24/2024
1.3.2 105 9/6/2024
1.3.1 105 9/5/2024
1.3.0 129 8/11/2024
1.2.1 106 7/8/2024
1.2.0 315 11/12/2023
1.1.1 199 11/4/2023
1.1.0 250 11/4/2023
1.0.4 270 11/1/2023
1.0.3 208 10/31/2023
1.0.2 270 10/26/2023
1.0.1 228 10/23/2023
1.0.0 243 10/22/2023