Show / Hide Table of Contents

    Class Either<TLeft, TRight>

    Represents a 2-type discriminate union with Left and Right components.
    Inheritance
    System.Object
    Either<TLeft, TRight>
    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 Either<TLeft, TRight>
    Type Parameters
    Name Description
    TLeft Left type.
    TRight Right type.
    Examples
    The following example assigns values to and extracts them from an Either<int, string>.
    using Maki;
    using System;
    
    namespace Samples
    {
        class Program
        {
            static void PrintEither(Either<int, string> either)
            {
                // If either contains the left type (int)
                if (either.IsLeft)
                {
                    // Output the left (int) value to the console
                    Console.WriteLine(either.GetLeft());
                }
                else // either.IsRight
                {
                    // Output the right (string) value to the console
                    Console.WriteLine(either.GetRight());
                }
            }
    
            static void Main(string[] args)
            {
                // Assign an int
                Either<int, string> either = 42;
    
                // Will print "42"
                PrintEither(either);
    
                either = "Hello world!";
    
                // Will print "Hello world!"
                PrintEither(either);
            }
        }
    }

    Constructors

    | Improve this Doc View Source

    Either(TLeft)

    Creates a new instance of Either from an item of type TLeft.
    Declaration
    public Either(TLeft left)
    Parameters
    Type Name Description
    TLeft left Item of type TLeft
    | Improve this Doc View Source

    Either(TRight)

    Creates a new instance of Either from an item of type TRight
    Declaration
    public Either(TRight right)
    Parameters
    Type Name Description
    TRight right Item of type TRight

    Properties

    | Improve this Doc View Source

    IsLeft

    True if Left is inhabited.
    Declaration
    public bool IsLeft { get; }
    Property Value
    Type Description
    System.Boolean
    | Improve this Doc View Source

    IsRight

    True if Right is inhabited.
    Declaration
    public bool IsRight { get; }
    Property Value
    Type Description
    System.Boolean

    Methods

    | Improve this Doc View Source

    Equals(Object)

    Determines whether the specified object is equal to the current object.
    Declaration
    public override bool Equals(object obj)
    Parameters
    Type Name Description
    System.Object obj The object to compare with the current object.
    Returns
    Type Description
    System.Boolean True if the objects are equal.
    Overrides
    System.Object.Equals(System.Object)
    | Improve this Doc View Source

    GetHashCode()

    Returns the hash code for this instance.
    Declaration
    public override int GetHashCode()
    Returns
    Type Description
    System.Int32 Hash code.
    Overrides
    System.Object.GetHashCode()
    | Improve this Doc View Source

    GetLeft()

    Gets the Left component.
    Declaration
    public TLeft GetLeft()
    Returns
    Type Description
    TLeft
    Exceptions
    Type Condition
    System.InvalidCastException Thrown on get if the inhabiting object is Right.
    | Improve this Doc View Source

    GetRight()

    Gets the Right component.
    Declaration
    public TRight GetRight()
    Returns
    Type Description
    TRight
    Exceptions
    Type Condition
    System.InvalidCastException Thrown on get if the inhabiting object is Left.
    | Improve this Doc View Source

    MakeLeft(TLeft)

    Creates a new Either explicitly placing the item in the Left component.
    Declaration
    public static Either<TLeft, TRight> MakeLeft(TLeft item)
    Parameters
    Type Name Description
    TLeft item Item to place in the Either.
    Returns
    Type Description
    Either<TLeft, TRight> Either containing the item.
    Remarks
    Use this method when both TLeft and TRight are the same.
    | Improve this Doc View Source

    MakeRight(TRight)

    Creates a new Either explicitly placing the item in the Right component.
    Declaration
    public static Either<TLeft, TRight> MakeRight(TRight item)
    Parameters
    Type Name Description
    TRight item Item to place in the Either.
    Returns
    Type Description
    Either<TLeft, TRight> Either containing the item.
    Remarks
    Use this method when both TLeft and TRight are the same.
    | Improve this Doc View Source

    Set(TLeft)

    Sets the Left component.
    Declaration
    public void Set(TLeft left)
    Parameters
    Type Name Description
    TLeft left Item to place in Either.
    | Improve this Doc View Source

    Set(TRight)

    Sets the Right component.
    Declaration
    public void Set(TRight right)
    Parameters
    Type Name Description
    TRight right Item to place in Either.
    | Improve this Doc View Source

    SetLeft(TLeft)

    Explicitly sets the Left component.
    Declaration
    public void SetLeft(TLeft left)
    Parameters
    Type Name Description
    TLeft left Item to place in Either.
    Remarks
    Use whent TLeft and TRight are of the same type.
    | Improve this Doc View Source

    SetRight(TRight)

    Explicitly sets the Right component.
    Declaration
    public void SetRight(TRight right)
    Parameters
    Type Name Description
    TRight right Item to place in Either.
    Remarks
    Use whent TLeft and TRight are of the same type.

    Operators

    | Improve this Doc View Source

    Explicit(Either<TLeft, TRight> to TLeft)

    Explicit cast from Either to TLeft.
    Declaration
    public static explicit operator TLeft(Either<TLeft, TRight> either)
    Parameters
    Type Name Description
    Either<TLeft, TRight> either Either object to cast.
    Returns
    Type Description
    TLeft
    Exceptions
    Type Condition
    System.InvalidCastException Thrown when the inhabiting object of the either is Right.
    | Improve this Doc View Source

    Explicit(Either<TLeft, TRight> to TRight)

    Explicit cast from Either to TRight.
    Declaration
    public static explicit operator TRight(Either<TLeft, TRight> either)
    Parameters
    Type Name Description
    Either<TLeft, TRight> either Either object to cast.
    Returns
    Type Description
    TRight
    Exceptions
    Type Condition
    System.InvalidCastException Thrown when the inhabiting object of the either is Left.
    | Improve this Doc View Source

    Implicit(TLeft to Either<TLeft, TRight>)

    Implicit cast from TLeft item to Either. Creates a new Either.
    Declaration
    public static implicit operator Either<TLeft, TRight>(TLeft item)
    Parameters
    Type Name Description
    TLeft item Item to place in Either.
    Returns
    Type Description
    Either<TLeft, TRight>
    | Improve this Doc View Source

    Implicit(TRight to Either<TLeft, TRight>)

    Implicit cast from TRight item to Either. Creates a new Either.
    Declaration
    public static implicit operator Either<TLeft, TRight>(TRight item)
    Parameters
    Type Name Description
    TRight item Item to place in Either.
    Returns
    Type Description
    Either<TLeft, TRight>
    • Improve this Doc
    • View Source
    Back to top Generated by DocFX