Dersei.Zorya 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Dersei.Zorya --version 1.0.0
NuGet\Install-Package Dersei.Zorya -Version 1.0.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="Dersei.Zorya" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dersei.Zorya --version 1.0.0
#r "nuget: Dersei.Zorya, 1.0.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 Dersei.Zorya as a Cake Addin
#addin nuget:?package=Dersei.Zorya&version=1.0.0

// Install Dersei.Zorya as a Cake Tool
#tool nuget:?package=Dersei.Zorya&version=1.0.0

Zorya

C# implementation of variant type. Implementation of variant type both as a class (Variant) and a structure (ValueVariant). Both types supports up to eight elements and implement implicit cast operators.

Using a variant

ValueVariant<int> v = 42;
ValueVariant<int, string> v = "42";
ValueVariant<int, string, double> v = 42.0;

or

Variant<int> v = 42;
Variant<int, string> v = "42";
Variant<int, string, double> v = 10.0;

It's also possible to use a constructor:

ValueVariant<int, double, string> v = new (42);

Getting a value

Getting a value is possible with Get method which throws BadVariantAccessException or BadValueVariantAccessException if there's no element of the requested type:

Variant<int, string, double> v = 42.0;
v.Get<double>();

or with TryGet which returns true if the element exists or false if it doesn't:

ValueVariant<int, string, double, long> v = 42L;
if (v.TryGet(out long value))
{
    return value * 2;
}

Using a value

Additionally there are also Match, TryMatch and MatchOrDefault methods receiving Action or Func. They apply given function on the element of requested type. Match throws BadVariantAccessException or BadValueVariantAccessException, TryMatch returns true if the element exists or false if it doesn't. MatchOrDefault executes given fallback action in case of MatchOrDefault(Action) or returns given default value in case of MatchOrDefault(Func).

Variant<int, string, double> v = 42.0;
v.Match((int i) => Console.WriteLine($"int {i}"));
Variant<int, string> v2 = "42";
v2.MatchOrDefault((int i) => Console.WriteLine($"int {i}"), () => Console.WriteLine("Incorrect type"));
Variant<int, double> v3 = 42.0;
if(v3.TryMatch((int i) => i * 10, out var result))
{
    Console.WriteLine(result);
}

Visit

Visit method accepts one function for all types the variant may contain and then executes the one corresponding to the set type.

Variant<int, double, string> v = 42.0;
v.Visit((int i) => Console.WriteLine($"int {i}"), (double d) => Console.WriteLine($"double {d}", (string s) => Console.WriteLine($"string {s}");

Additional capabilities

It's also possible to request the set type using GetSetType:

Variant<int, string, double, Point> v = new Point(0, 1);
if(v.GetSetType() == typeof(Point))
{
}

Variant also allows to set a new element without creating a new object using Set method which returns true if successful:

Variant<int, string> v = new (10);
var wasSetSuccess = v.Set("20");
Debug.Assert(v.Get<string>() == "20"); //true
Debug.Assert(wasSetSuccess); //true

Get, TryGet, Match, TryMarch, MatchOrDefault methods are also define as static on individual types and Variant, and ValueVariant classes:

Variant<int, string, double, long> v = 42L;
var l = Variant.Get<long>(v);

or

Variant<int, string, double, long> v = 42L;
var l = Variant<int, string, double, long>.Get<long>(v);

Example of use case

Variant<int, double, string> ParseInput(string input)
{
    if (int.TryParse(input, out var i))
    {
        return i;
    }
    if (double.TryParse(input, out var d))
    {
        return d;
    }
    return input;
}

Name

Zorya (lit. "Dawn"; also many variants: Zarya, Zara, Zaranitsa, Zoryushka, etc.) is a figure in Slavic folklore, a feminine personification of dawn, possibly goddess. Depending on tradition, she may appear as a singular entity, often called "The Red Maiden", or two or three sisters at once. Zorya - Wikipedia.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.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 404 10/25/2022
1.0.1 371 10/19/2022
1.0.0 352 9/12/2022