Skip to content

A set of common type unions that work on current .NET versions.

License

Notifications You must be signed in to change notification settings

DotNetExtension/CommonTypeUnions

Repository files navigation

GitHub license Contributor Covenant GitHub issues

Common Union Types

Warning

Type Unions are in the early proposed stage and will likely see changes as time goes on, we plan to keep up with these changes as much as possible to ensure the easiest migration path once type unions make it into a stable .net version.

Getting Started

You can get started using these types by simply installing the CommonTypeUnions package from nuget.

Alternatively you can install it using the dotnet cli:

dotnet add package CommonTypeUnions --version 1.0.2

Option<TValue>

Option is a struct union found in many languages, it can either represent a value that is something (Some) or nothing (None).

using CommonTypeUnions.Unions;
using static CommonTypeUnions.Extensions.OptionExtensions;

Option<string> x = Some("text");
Option<string> y = None();

if (x.IsSome(out var value))
{
    Console.WriteLine($"Value: {value}");
}

var isNone = y.IsNone();

Console.WriteLine($"Is None: {isNone}");

Output:

Value: text
Is None: True

Result<TValue, TError>

Option is a struct union found in many languages, it is used to either represent a successful result (Success) or error (Failure) from a function.

using CommonTypeUnions.Unions;
using static CommonTypeUnions.Extensions.ResultExtensions;

Result<int, string> x = Success(42);
Result<int, string> y = Failure("Oh no");

if (x.IsSuccess(out var value))
{
    Console.WriteLine($"Value: {value}");
}

if (y.IsFailure(out var error))
{
    Console.WriteLine($"Failure: {error}");
}

Output:

Value: 42
Failure: Oh no

About

A set of common type unions that work on current .NET versions.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages