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.
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?`.
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.
Issues and pull requests are welcome to help this repository be the best it can be.
This repository falls under the MPL-2 license.