Class Variant<T1, T2, T3, T4, T5>
Represents a 5-type discriminate union.
Inheritance
System.Object
Variant<T1, T2, T3, T4, T5>
Inherited Members
System.Object.Equals(System.Object, System.Object)
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: Maki
Assembly: Maki.dll
Syntax
public sealed class Variant<T1, T2, T3, T4, T5> : VariantBase
Type Parameters
| Name |
Description |
| T1 |
Represents the variant's first type. |
| T2 |
Represents the variant's second type. |
| T3 |
Represents the variant's third type. |
| T4 |
Represents the variant's fourth type. |
| T5 |
Represents the variant's fifth type. |
Examples
The following example shows how to assign values of different types to a Variant and how to extract
values from it.
using Maki;
using System;
namespace Samples
{
class Program
{
static void Main(string[] args)
{
// A variant can hold a value of any of its generic types
Variant<int, string, double> variant = 42;
// Depending on the type of the value currently inhabiting the variant,
// the right Action gets executed
variant.Apply(
i => Console.WriteLine(i * 2),
s => Console.WriteLine(s + "!"),
d => Console.WriteLine($"Double: {d}")
);
// Can reassign variant with another of its generic types
variant = "Hello world";
// Check the type of the value currently inhabiting the variant
if (variant.Is<string>())
{
// Extract a string from the variant
Console.WriteLine($"The string is: {variant.Get<string>()}");
}
}
}
}
Constructors
|
Improve this Doc
View Source
Variant(T1)
Creates a new Variant instance from an item of type T1.
Declaration
Parameters
| Type |
Name |
Description |
| T1 |
item |
Item of type T1. |
|
Improve this Doc
View Source
Variant(T2)
Creates a new Variant instance from an item of type T2.
Declaration
Parameters
| Type |
Name |
Description |
| T2 |
item |
Item of type T2. |
|
Improve this Doc
View Source
Variant(T3)
Creates a new Variant instance from an item of type T3.
Declaration
Parameters
| Type |
Name |
Description |
| T3 |
item |
Item of type T3. |
|
Improve this Doc
View Source
Variant(T4)
Creates a new Variant instance from an item of type T4.
Declaration
Parameters
| Type |
Name |
Description |
| T4 |
item |
Item of type T4. |
|
Improve this Doc
View Source
Variant(T5)
Creates a new Variant instance from an item of type T5.
Declaration
Parameters
| Type |
Name |
Description |
| T5 |
item |
Item of type T5. |
Methods
|
Improve this Doc
View Source
Make1(T1)
Creates a new Variant explicitly placing the item as the first type
(T1).
Declaration
public static Variant<T1, T2, T3, T4, T5> Make1(T1 item)
Parameters
| Type |
Name |
Description |
| T1 |
item |
Item to place in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
New Variant instance. |
|
Improve this Doc
View Source
Make2(T2)
Creates a new Variant explicitly placing the item as the second type
(T2).
Declaration
public static Variant<T1, T2, T3, T4, T5> Make2(T2 item)
Parameters
| Type |
Name |
Description |
| T2 |
item |
Item to place in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
New Variant instance. |
|
Improve this Doc
View Source
Make3(T3)
Creates a new Variant explicitly placing the item as the third type
(T3).
Declaration
public static Variant<T1, T2, T3, T4, T5> Make3(T3 item)
Parameters
| Type |
Name |
Description |
| T3 |
item |
Item to place in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
New Variant instance. |
|
Improve this Doc
View Source
Make4(T4)
Creates a new Variant explicitly placing the item as the fourth type
(T4).
Declaration
public static Variant<T1, T2, T3, T4, T5> Make4(T4 item)
Parameters
| Type |
Name |
Description |
| T4 |
item |
Item to place in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
New Variant instance. |
|
Improve this Doc
View Source
Make5(T5)
Creates a new Variant explicitly placing the item as the fifth type
(T5).
Declaration
public static Variant<T1, T2, T3, T4, T5> Make5(T5 item)
Parameters
| Type |
Name |
Description |
| T5 |
item |
Item to place in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
New Variant instance. |
Operators
|
Improve this Doc
View Source
Explicit(Variant<T1, T2, T3, T4, T5> to T1)
Explicitly casts from variant to T1.
Declaration
public static explicit operator T1(Variant<T1, T2, T3, T4, T5> variant)
Parameters
| Type |
Name |
Description |
| Variant<T1, T2, T3, T4, T5> |
variant |
Variant to cast to T1. |
Returns
Exceptions
| Type |
Condition |
| System.InvalidCastException |
Thrown if the item inhabiting the variant is not of type |
|
Improve this Doc
View Source
Explicit(Variant<T1, T2, T3, T4, T5> to T2)
Explicitly casts from variant to T2.
Declaration
public static explicit operator T2(Variant<T1, T2, T3, T4, T5> variant)
Parameters
| Type |
Name |
Description |
| Variant<T1, T2, T3, T4, T5> |
variant |
Variant to cast to T2. |
Returns
Exceptions
| Type |
Condition |
| System.InvalidCastException |
Thrown if the item inhabiting the variant is not of type |
|
Improve this Doc
View Source
Explicit(Variant<T1, T2, T3, T4, T5> to T3)
Explicitly casts from variant to T3.
Declaration
public static explicit operator T3(Variant<T1, T2, T3, T4, T5> variant)
Parameters
| Type |
Name |
Description |
| Variant<T1, T2, T3, T4, T5> |
variant |
Variant to cast to T3. |
Returns
Exceptions
| Type |
Condition |
| System.InvalidCastException |
Thrown if the item inhabiting the variant is not of type |
|
Improve this Doc
View Source
Explicit(Variant<T1, T2, T3, T4, T5> to T4)
Explicitly casts from variant to T4.
Declaration
public static explicit operator T4(Variant<T1, T2, T3, T4, T5> variant)
Parameters
| Type |
Name |
Description |
| Variant<T1, T2, T3, T4, T5> |
variant |
Variant to cast to T4. |
Returns
Exceptions
| Type |
Condition |
| System.InvalidCastException |
Thrown if the item inhabiting the variant is not of type |
|
Improve this Doc
View Source
Explicit(Variant<T1, T2, T3, T4, T5> to T5)
Explicitly casts from variant to T5.
Declaration
public static explicit operator T5(Variant<T1, T2, T3, T4, T5> variant)
Parameters
| Type |
Name |
Description |
| Variant<T1, T2, T3, T4, T5> |
variant |
Variant to cast to T5. |
Returns
Exceptions
| Type |
Condition |
| System.InvalidCastException |
Thrown if the item inhabiting the variant is not of type |
|
Improve this Doc
View Source
Implicit(T1 to Variant<T1, T2, T3, T4, T5>)
Implicitly casts from T1 to variant. Creates a new Variant
inhabited by the given item.
Declaration
public static implicit operator Variant<T1, T2, T3, T4, T5>(T1 item)
Parameters
| Type |
Name |
Description |
| T1 |
item |
Item to store in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
|
|
Improve this Doc
View Source
Implicit(T2 to Variant<T1, T2, T3, T4, T5>)
Implicitly casts from T2 to variant. Creates a new Variant
inhabited by the given item.
Declaration
public static implicit operator Variant<T1, T2, T3, T4, T5>(T2 item)
Parameters
| Type |
Name |
Description |
| T2 |
item |
Item to store in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
|
|
Improve this Doc
View Source
Implicit(T3 to Variant<T1, T2, T3, T4, T5>)
Implicitly casts from T3 to variant. Creates a new Variant
inhabited by the given item.
Declaration
public static implicit operator Variant<T1, T2, T3, T4, T5>(T3 item)
Parameters
| Type |
Name |
Description |
| T3 |
item |
Item to store in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
|
|
Improve this Doc
View Source
Implicit(T4 to Variant<T1, T2, T3, T4, T5>)
Implicitly casts from T4 to variant. Creates a new Variant
inhabited by the given item.
Declaration
public static implicit operator Variant<T1, T2, T3, T4, T5>(T4 item)
Parameters
| Type |
Name |
Description |
| T4 |
item |
Item to store in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
|
|
Improve this Doc
View Source
Implicit(T5 to Variant<T1, T2, T3, T4, T5>)
Implicitly casts from T5 to variant. Creates a new Variant
inhabited by the given item.
Declaration
public static implicit operator Variant<T1, T2, T3, T4, T5>(T5 item)
Parameters
| Type |
Name |
Description |
| T5 |
item |
Item to store in the variant. |
Returns
| Type |
Description |
| Variant<T1, T2, T3, T4, T5> |
|
Extension Methods
VariantExtensions.Map<T1, T2, T3, T4, T5, U1, U2, U3, U4, U5>(Variant<T1, T2, T3, T4, T5>, Func<T1, U1>, Func<T2, U2>, Func<T3, U3>, Func<T4, U4>, Func<T5, U5>)
VariantExtensions.Apply<T1, T2, T3, T4, T5, U>(Variant<T1, T2, T3, T4, T5>, Func<T1, U>, Func<T2, U>, Func<T3, U>, Func<T4, U>, Func<T5, U>)
VariantExtensions.Apply<T1, T2, T3, T4, T5>(Variant<T1, T2, T3, T4, T5>, Action<T1>, Action<T2>, Action<T3>, Action<T4>, Action<T5>)