Class Variant<T1, T2>
Represents a 2-type discriminate union.
Inheritance
System.Object
Variant<T1, T2>
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> : VariantBase
Type Parameters
Name |
Description |
T1 |
Represents the variant's first type. |
T2 |
Represents the variant's second 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 . |
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> Make1(T1 item)
Parameters
Type |
Name |
Description |
T1 |
item |
Item to place in the variant. |
Returns
Type |
Description |
Variant<T1, T2> |
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> Make2(T2 item)
Parameters
Type |
Name |
Description |
T2 |
item |
Item to place in the variant. |
Returns
Type |
Description |
Variant<T1, T2> |
New Variant instance. |
Operators
|
Improve this Doc
View Source
Explicit(Variant<T1, T2> to T1)
Explicitly casts from variant to T1
.
Declaration
public static explicit operator T1(Variant<T1, T2> variant)
Parameters
Type |
Name |
Description |
Variant<T1, T2> |
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> to T2)
Explicitly casts from variant to T2
.
Declaration
public static explicit operator T2(Variant<T1, T2> variant)
Parameters
Type |
Name |
Description |
Variant<T1, T2> |
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
Implicit(T1 to Variant<T1, T2>)
Implicitly casts from T1
to variant. Creates a new Variant
inhabited by the given item.
Declaration
public static implicit operator Variant<T1, T2>(T1 item)
Parameters
Type |
Name |
Description |
T1 |
item |
Item to store in the variant. |
Returns
|
Improve this Doc
View Source
Implicit(T2 to Variant<T1, T2>)
Implicitly casts from T2
to variant. Creates a new Variant
inhabited by the given item.
Declaration
public static implicit operator Variant<T1, T2>(T2 item)
Parameters
Type |
Name |
Description |
T2 |
item |
Item to store in the variant. |
Returns
Extension Methods