Class Either<TLeft, TRight>
Represents a value that can be one of two types.
Inheritance
Either<TLeft, TRight>
Assembly: Recore.dll
Syntax
[JsonConverter(typeof(EitherConverter))]
public sealed class Either<TLeft, TRight> : IEquatable<Either<TLeft, TRight>>
Type Parameters
Name |
Description |
TLeft |
|
TRight |
|
Examples
Either<TLeft, TRight> creates a type with a value that can be one of two types.
In this, it's similar to Optional<T>,
which you can think of like Either<T, null>
with some extra functionality.
If you're familiar with TypeScript, you can think of Either<TLeft, TRight> as a union type:
TLeft | TRight
You create an instance of Either<TLeft, TRight> with one of its constructors, but the implicit conversion operator is pretty convenient too:
Either<string, int> either = "hello";
Like Optional<T>, the main way to work with
Either<TLeft, TRight> is with
Switch()
:
either.Switch(
l => Console.WriteLine($"Value is a string: {l}"),
r => Console.WriteLine($"Value is an int: {r}"));
you can also return a value:
var message = either.Switch(
l => $"Value is a string: {l}",
r => $"Value is an int: {r}");
Compared to Optional<T>, though,
where Switch()
is more of a last resort when no higher-level idiom is available,
Either<TLeft, TRight>
leans on Switch()
heavily.
You can think of Either<TLeft, TRight> as being lower-level than
Optional<T>.
Either<TLeft, TRight> also has
OnLeft()
and OnRight()
,
analogous to Optional<T>'s
OnValue()
. (Note that there's no OnEmpty()
; you can just use if (!opt.HasValue)
for that.)
Either<string, bool> newEither = either.OnRight(x => x > 0);
Constructors
|
Improve this Doc
View Source
Either(TLeft)
Constructs an instance of the type from a value of TLeft
.
Declaration
public Either(TLeft left)
Parameters
Type |
Name |
Description |
TLeft |
left |
|
|
Improve this Doc
View Source
Either(TRight)
Constructs an instance of the type from a value of TRight
.
Declaration
public Either(TRight right)
Parameters
Type |
Name |
Description |
TRight |
right |
|
Properties
|
Improve this Doc
View Source
IsLeft
Indicates whether the value is of type TLeft
.
Declaration
public bool IsLeft { get; }
Property Value
|
Improve this Doc
View Source
IsRight
Indicates whether the value is of type TRight
.
Declaration
public bool IsRight { get; }
Property Value
Methods
|
Improve this Doc
View Source
Equals(Either<TLeft, TRight>)
Declaration
public bool Equals(Either<TLeft, TRight> other)
Parameters
Type |
Name |
Description |
Either<TLeft, TRight> |
other |
|
Returns
|
Improve this Doc
View Source
Equals(Either<TLeft, TRight>, IEqualityComparer<TLeft>, IEqualityComparer<TRight>)
Declaration
public bool Equals(Either<TLeft, TRight> other, IEqualityComparer<TLeft> leftComparer, IEqualityComparer<TRight> rightComparer)
Parameters
Returns
|
Improve this Doc
View Source
Equals(Object)
Declaration
public override bool Equals(object obj)
Parameters
Type |
Name |
Description |
Object |
obj |
|
Returns
Overrides
|
Improve this Doc
View Source
GetHashCode()
Returns the hash code of the underlying value.
Declaration
public override int GetHashCode()
Returns
Overrides
|
Improve this Doc
View Source
GetLeft()
Declaration
public Optional<TLeft> GetLeft()
Returns
|
Improve this Doc
View Source
GetRight()
Declaration
public Optional<TRight> GetRight()
Returns
|
Improve this Doc
View Source
IfLeft(Action<TLeft>)
Takes an action only if the value is an instance of TLeft
.
Declaration
public void IfLeft(Action<TLeft> onLeft)
Parameters
Type |
Name |
Description |
Action<TLeft> |
onLeft |
|
|
Improve this Doc
View Source
IfRight(Action<TRight>)
Takes an action only if the value is an instance of TRight
.
Declaration
public void IfRight(Action<TRight> onRight)
Parameters
Type |
Name |
Description |
Action<TRight> |
onRight |
|
|
Improve this Doc
View Source
OnLeft<TResult>(Func<TLeft, TResult>)
Declaration
public Either<TResult, TRight> OnLeft<TResult>(Func<TLeft, TResult> onLeft)
Parameters
Type |
Name |
Description |
Func<TLeft, TResult> |
onLeft |
|
Returns
Type |
Description |
Either<TResult, TRight> |
|
Type Parameters
|
Improve this Doc
View Source
OnRight<TResult>(Func<TRight, TResult>)
Declaration
public Either<TLeft, TResult> OnRight<TResult>(Func<TRight, TResult> onRight)
Parameters
Type |
Name |
Description |
Func<TRight, TResult> |
onRight |
|
Returns
Type |
Description |
Either<TLeft, TResult> |
|
Type Parameters
|
Improve this Doc
View Source
Swap()
Declaration
public Either<TRight, TLeft> Swap()
Returns
Type |
Description |
Either<TRight, TLeft> |
|
|
Improve this Doc
View Source
Switch(Action<TLeft>, Action<TRight>)
Takes one of two actions depending on the underlying value.
Declaration
public void Switch(Action<TLeft> onLeft, Action<TRight> onRight)
Parameters
Type |
Name |
Description |
Action<TLeft> |
onLeft |
|
Action<TRight> |
onRight |
|
|
Improve this Doc
View Source
Switch<T>(Func<TLeft, T>, Func<TRight, T>)
Calls one of two functions depending on the underlying value.
Declaration
public T Switch<T>(Func<TLeft, T> onLeft, Func<TRight, T> onRight)
Parameters
Type |
Name |
Description |
Func<TLeft, T> |
onLeft |
|
Func<TRight, T> |
onRight |
|
Returns
Type Parameters
|
Improve this Doc
View Source
ToString()
Returns the string representation of the underlying value.
Declaration
public override string ToString()
Returns
Overrides
Operators
|
Improve this Doc
View Source
Equality(Either<TLeft, TRight>, Either<TLeft, TRight>)
Declaration
public static bool operator ==(Either<TLeft, TRight> lhs, Either<TLeft, TRight> rhs)
Parameters
Type |
Name |
Description |
Either<TLeft, TRight> |
lhs |
|
Either<TLeft, TRight> |
rhs |
|
Returns
|
Improve this Doc
View Source
Implicit(TLeft to Either<TLeft, TRight>)
Declaration
public static implicit operator Either<TLeft, TRight>(TLeft left)
Parameters
Type |
Name |
Description |
TLeft |
left |
|
Returns
Type |
Description |
Either<TLeft, TRight> |
|
|
Improve this Doc
View Source
Implicit(TRight to Either<TLeft, TRight>)
Declaration
public static implicit operator Either<TLeft, TRight>(TRight right)
Parameters
Type |
Name |
Description |
TRight |
right |
|
Returns
Type |
Description |
Either<TLeft, TRight> |
|
|
Improve this Doc
View Source
Inequality(Either<TLeft, TRight>, Either<TLeft, TRight>)
Declaration
public static bool operator !=(Either<TLeft, TRight> lhs, Either<TLeft, TRight> rhs)
Parameters
Type |
Name |
Description |
Either<TLeft, TRight> |
lhs |
|
Either<TLeft, TRight> |
rhs |
|
Returns
Implements
Extension Methods