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 SourceEither(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 |
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 SourceIsLeft
True if Left is inhabited.
Declaration
public bool IsLeft { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
IsRight
True if Right is inhabited.
Declaration
public bool IsRight { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
Methods
| Improve this Doc View SourceEquals(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. |
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. |
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
|
Improve this Doc
View Source
TLeft and TRight are the same.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
|
Improve this Doc
View Source
TLeft and TRight are the same.Set(TLeft)
Sets the Left component.
Declaration
public void Set(TLeft left)
Parameters
| Type | Name | Description |
|---|---|---|
| TLeft | left | Item to place in Either. |
Set(TRight)
Sets the Right component.
Declaration
public void Set(TRight right)
Parameters
| Type | Name | Description |
|---|---|---|
| TRight | right | Item to place in Either. |
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
|
Improve this Doc
View Source
TLeft and TRight are of
the same type.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 SourceExplicit(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. |
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. |
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> |
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> |