Class Variant<T1, T2, T3, T4, T5, T6, T7>
Represents a 7-type discriminate union.
Inheritance
System.Object
Variant<T1, T2, T3, T4, T5, T6, T7>
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, T6, T7> : 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. |
T6 |
Represents the variant's sixth type. |
T7 |
Represents the variant's seventh 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 . |
|
Improve this Doc
View Source
Variant(T6)
Creates a new Variant instance from an item of type T6
.
Declaration
Parameters
Type |
Name |
Description |
T6 |
item |
Item of type T6 . |
|
Improve this Doc
View Source
Variant(T7)
Creates a new Variant instance from an item of type T7
.
Declaration
Parameters
Type |
Name |
Description |
T7 |
item |
Item of type T7 . |
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, T6, T7> Make1(T1 item)
Parameters
Type |
Name |
Description |
T1 |
item |
Item to place in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
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, T6, T7> Make2(T2 item)
Parameters
Type |
Name |
Description |
T2 |
item |
Item to place in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
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, T6, T7> Make3(T3 item)
Parameters
Type |
Name |
Description |
T3 |
item |
Item to place in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
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, T6, T7> Make4(T4 item)
Parameters
Type |
Name |
Description |
T4 |
item |
Item to place in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
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, T6, T7> Make5(T5 item)
Parameters
Type |
Name |
Description |
T5 |
item |
Item to place in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
New Variant instance. |
|
Improve this Doc
View Source
Make6(T6)
Creates a new Variant explicitly placing the item as the sixth type
(T6
).
Declaration
public static Variant<T1, T2, T3, T4, T5, T6, T7> Make6(T6 item)
Parameters
Type |
Name |
Description |
T6 |
item |
Item to place in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
New Variant instance. |
|
Improve this Doc
View Source
Make7(T7)
Creates a new Variant explicitly placing the item as the seventh type
(T7
).
Declaration
public static Variant<T1, T2, T3, T4, T5, T6, T7> Make7(T7 item)
Parameters
Type |
Name |
Description |
T7 |
item |
Item to place in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
New Variant instance. |
Operators
|
Improve this Doc
View Source
Explicit(Variant<T1, T2, T3, T4, T5, T6, T7> to T1)
Explicitly casts from variant to T1
.
Declaration
public static explicit operator T1(Variant<T1, T2, T3, T4, T5, T6, T7> variant)
Parameters
Type |
Name |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
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, T6, T7> to T2)
Explicitly casts from variant to T2
.
Declaration
public static explicit operator T2(Variant<T1, T2, T3, T4, T5, T6, T7> variant)
Parameters
Type |
Name |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
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, T6, T7> to T3)
Explicitly casts from variant to T3
.
Declaration
public static explicit operator T3(Variant<T1, T2, T3, T4, T5, T6, T7> variant)
Parameters
Type |
Name |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
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, T6, T7> to T4)
Explicitly casts from variant to T4
.
Declaration
public static explicit operator T4(Variant<T1, T2, T3, T4, T5, T6, T7> variant)
Parameters
Type |
Name |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
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, T6, T7> to T5)
Explicitly casts from variant to T5
.
Declaration
public static explicit operator T5(Variant<T1, T2, T3, T4, T5, T6, T7> variant)
Parameters
Type |
Name |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
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
Explicit(Variant<T1, T2, T3, T4, T5, T6, T7> to T6)
Explicitly casts from variant to T6
.
Declaration
public static explicit operator T6(Variant<T1, T2, T3, T4, T5, T6, T7> variant)
Parameters
Type |
Name |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
variant |
Variant to cast to T6 . |
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, T6, T7> to T7)
Explicitly casts from variant to T7
.
Declaration
public static explicit operator T7(Variant<T1, T2, T3, T4, T5, T6, T7> variant)
Parameters
Type |
Name |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
variant |
Variant to cast to T7 . |
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, T6, T7>)
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, T6, T7>(T1 item)
Parameters
Type |
Name |
Description |
T1 |
item |
Item to store in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
|
|
Improve this Doc
View Source
Implicit(T2 to Variant<T1, T2, T3, T4, T5, T6, T7>)
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, T6, T7>(T2 item)
Parameters
Type |
Name |
Description |
T2 |
item |
Item to store in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
|
|
Improve this Doc
View Source
Implicit(T3 to Variant<T1, T2, T3, T4, T5, T6, T7>)
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, T6, T7>(T3 item)
Parameters
Type |
Name |
Description |
T3 |
item |
Item to store in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
|
|
Improve this Doc
View Source
Implicit(T4 to Variant<T1, T2, T3, T4, T5, T6, T7>)
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, T6, T7>(T4 item)
Parameters
Type |
Name |
Description |
T4 |
item |
Item to store in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
|
|
Improve this Doc
View Source
Implicit(T5 to Variant<T1, T2, T3, T4, T5, T6, T7>)
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, T6, T7>(T5 item)
Parameters
Type |
Name |
Description |
T5 |
item |
Item to store in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
|
|
Improve this Doc
View Source
Implicit(T6 to Variant<T1, T2, T3, T4, T5, T6, T7>)
Implicitly casts from T6
to variant. Creates a new Variant
inhabited by the given item.
Declaration
public static implicit operator Variant<T1, T2, T3, T4, T5, T6, T7>(T6 item)
Parameters
Type |
Name |
Description |
T6 |
item |
Item to store in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
|
|
Improve this Doc
View Source
Implicit(T7 to Variant<T1, T2, T3, T4, T5, T6, T7>)
Implicitly casts from T7
to variant. Creates a new Variant
inhabited by the given item.
Declaration
public static implicit operator Variant<T1, T2, T3, T4, T5, T6, T7>(T7 item)
Parameters
Type |
Name |
Description |
T7 |
item |
Item to store in the variant. |
Returns
Type |
Description |
Variant<T1, T2, T3, T4, T5, T6, T7> |
|
Extension Methods
VariantExtensions.Map<T1, T2, T3, T4, T5, T6, T7, U1, U2, U3, U4, U5, U6, U7>(Variant<T1, T2, T3, T4, T5, T6, T7>, Func<T1, U1>, Func<T2, U2>, Func<T3, U3>, Func<T4, U4>, Func<T5, U5>, Func<T6, U6>, Func<T7, U7>)
VariantExtensions.Apply<T1, T2, T3, T4, T5, T6, T7, U>(Variant<T1, T2, T3, T4, T5, T6, T7>, Func<T1, U>, Func<T2, U>, Func<T3, U>, Func<T4, U>, Func<T5, U>, Func<T6, U>, Func<T7, U>)
VariantExtensions.Apply<T1, T2, T3, T4, T5, T6, T7>(Variant<T1, T2, T3, T4, T5, T6, T7>, Action<T1>, Action<T2>, Action<T3>, Action<T4>, Action<T5>, Action<T6>, Action<T7>)