Ooak 0.1.0

dotnet add package Ooak --version 0.1.0
                    
NuGet\Install-Package Ooak -Version 0.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="Ooak" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Ooak" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Ooak" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Ooak --version 0.1.0
                    
#r "nuget: Ooak, 0.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.
#:package Ooak@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Ooak&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Ooak&version=0.1.0
                    
Install as a Cake Tool

Ooak - The TypeUnion implementation

This package allows to use type unions in C#. A type union represents a value that can be one of two types.

Creating TypeUnion instances

Create TypeUnion instances:

/// <summary>
/// Tries to parse the date. Returns either a DateTime on success, the original string otherwise.
/// This example is stupid, but demonstrates how things work
/// </summary>
TypeUnion<DateTime, string> TryParseDate(string date)
{
    if (DateTime.TryParse(date, out var outDateTime))
    {
        // Return a `Left`, i.e. a `DateTime` instance
        return new TypeUnion<DateTime, string>.Left(outDateTime);
    }

    // Return a `Right`, i.e. a `DateTime` instance
    return new TypeUnion<DateTime, string>.Right(date);
}

Sometimes, the value you represent can be of two types at the same time (the anyOf case of the JSON deserialization):

// Yes, C#9 records are supported !
public record Paging(int Page, int ItemsPerPage);
public record Ordering(bool Ascending);

public void DemoBoth()
{
    TypeUnion<Paging, Ordering> queryParameters;
    // Can be left (Paging)
    queryParameters = new TypeUnion<Paging, Ordering>.Left(new Paging(1, 50));
    // Can be right (Ordering)
    queryParameters = new TypeUnion<Paging, Ordering>.Right(new Ordering(true));

    // But can also be both at the same time!
    queryParameters = new TypeUnion<Paging, Ordering>.Both(new Paging(10, 10), new Ordering(false));
}

Using the TypeUnion object

public void UsingTypeUnion(TypeUnion<Paging, Ordering> queryParameters)
{
    // Detect with `is` and the Left, Right and Both classes
    if (queryParameters is TypeUnion<Paging, Ordering>.Left p)
    {
        Console.WriteLine($"This is a Paging only at page {p.Value.Page} with {p.Value.ItemsPerPage} items per page");
    }

    // With pattern matching
    switch (queryParameters)
    {
        case TypeUnion<Paging, Ordering>.Left left:
            Console.WriteLine("Left");
            break;
        case TypeUnion<Paging, Ordering>.Right right:
            Console.WriteLine("Right");
            break;
        case TypeUnion<Paging, Ordering>.Both right:
            Console.WriteLine("Both");
            break;
    }

    // But if you need ordering info, you will need to test both Right and Both.
    // Or you could use this helper syntax :
    if (queryParameters.TryGetRight(out var ordering))
    {
        Console.WriteLine($"Ordering {(ordering.Ascending ? "Ascending" : "Descending")}");
    }
}

Special notes

The type parameters cannot contain nullable types (you cannot write TypeUnion<string?, DateTime> nor TypeUnion<string, DateTime?>), but you can make your whole union nullable (TypeUnion<string, DateTime>?).

This library only supports 2 type parameters for now. If you need more parameters, nest them : TypeUnion<A, TypeUnion<B, C>>.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen 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.1

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Ooak:

Package Downloads
Ooak.SystemTextJson

Ooak deserializer for System.Text.Json that allows the use of oneOf/anyOf/allOf

Ooak.NewtonsoftJson

Ooak deserializer for Newtonsoft.Json that allows the use of oneOf/anyOf/allOf

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 806 12/8/2021
0.0.3 692 12/8/2021
0.0.2 743 5/13/2021
0.0.1 714 5/13/2021