Show / Hide Table of Contents

    Class Variant<T1>

    Represents a 1-type discriminate union.
    Inheritance
    System.Object
    VariantBase
    Variant<T1>
    Inherited Members
    VariantBase.Index
    VariantBase.Is<T>()
    VariantBase.Get<T>()
    VariantBase.Get()
    VariantBase.Equals(Object)
    VariantBase.GetHashCode()
    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> : VariantBase
    Type Parameters
    Name Description
    T1 Represents the variant's first 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
    public Variant(T1 item)
    Parameters
    Type Name Description
    T1 item Item of type T1.

    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> Make1(T1 item)
    Parameters
    Type Name Description
    T1 item Item to place in the variant.
    Returns
    Type Description
    Variant<T1> New Variant instance.
    Remarks
    Use this method when the variant contains multiple instances of the same type. This allows explicit placing of the item.

    Operators

    | Improve this Doc View Source

    Explicit(Variant<T1> to T1)

    Explicitly casts from variant to T1.
    Declaration
    public static explicit operator T1(Variant<T1> variant)
    Parameters
    Type Name Description
    Variant<T1> variant Variant to cast to T1.
    Returns
    Type Description
    T1
    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>)

    Implicitly casts from T1 to variant. Creates a new Variant inhabited by the given item.
    Declaration
    public static implicit operator Variant<T1>(T1 item)
    Parameters
    Type Name Description
    T1 item Item to store in the variant.
    Returns
    Type Description
    Variant<T1>
    • Improve this Doc
    • View Source
    Back to top Generated by DocFX