Struct Either<T1, T2, T3, T4>
- Namespace
- SodaFlow.Functional
- Assembly
- SodaFlow.Functional.dll
A value which is one of four possibilities.
[PublicAPI]
public struct Either<T1, T2, T3, T4> : IEitherOfFour, IEither, IEquatable<Either<T1, T2, T3, T4>>
Type Parameters
T1The type of the first possibility.
T2The type of the second possibility.
T3The type of the third possibility.
T4The type of the fourth possibility.
- Implements
-
IEquatable<Either<T1, T2, T3, T4>>
- Inherited Members
- Extension Methods
Remarks
A discriminated union. The value is one of the four cases. Which one it is
is part of the value, and not something the caller must monitor next to it.
There is no property that hands the value out unchecked. Read it with
Match, or with one of the helpers on it, thus the code must answer
each case.
This is a struct, so default is a correct instance. It
holds the first case, with the default value of T1.
Methods
- Equals(Either<T1, T2, T3, T4>)
Gives true when the given instance is equal to this one.
- Equals(object?)
Gives true when the given object is an either of this same type which is equal to this one.
- First(T1)
Creates an either holding the given value as its first case.
- Fourth(T4)
Creates an either holding the given value as its fourth case.
- GetHashCode()
Gives a hash code that agrees with operator ==(Either<T1, T2, T3, T4>, Either<T1, T2, T3, T4>).
- IsFirst()
Gives true when this holds the first case.
- IsFourth()
Gives true when this holds the fourth case.
- IsSecond()
Gives true when this holds the second case.
- IsThird()
Gives true when this holds the third case.
- MapFirst<T>(Func<T1, T>)
Transforms the value if this holds the first case, and passes each other case through unchanged.
- MapFourth<T>(Func<T4, T>)
Transforms the value if this holds the fourth case, and passes each other case through unchanged.
- MapSecond<T>(Func<T2, T>)
Transforms the value if this holds the second case, and passes each other case through unchanged.
- MapThird<T>(Func<T3, T>)
Transforms the value if this holds the third case, and passes each other case through unchanged.
- MatchAsyncVoid(Func<T1, Task>, Func<T2, Task>, Func<T3, Task>, Func<T4, Task>)
Runs one of the four asynchronous actions depending on which case is held.
- MatchAsync<T>(Func<T1, Task<T>>, Func<T2, Task<T>>, Func<T3, Task<T>>, Func<T4, Task<T>>)
Runs one of the four asynchronous functions depending on which case is held, and returns its result.
- MatchVoid(Action<T1>, Action<T2>, Action<T3>, Action<T4>)
Runs one of the four actions depending on which case is held.
- Match<T>(Func<T1, T>, Func<T2, T>, Func<T3, T>, Func<T4, T>)
Runs one of the four functions depending on which case is held, and returns its result.
- Second(T2)
Creates an either holding the given value as its second case.
- Third(T3)
Creates an either holding the given value as its third case.
- ToString()
Returns a readable description of this instance, for diagnostics.
- TryGetFirst()
Gets the held value if this holds the first case.
- TryGetFourth()
Gets the held value if this holds the fourth case.
- TryGetSecond()
Gets the held value if this holds the second case.
- TryGetThird()
Gets the held value if this holds the third case.
Operators
- operator ==(Either<T1, T2, T3, T4>, Either<T1, T2, T3, T4>)
Gives true when two instances hold the same case with equal values.
- implicit operator Either<T1, T2, T3, T4>(EitherFirst<T1>)
Changes a value marked for the first position into an either of this type.
- implicit operator Either<T1, T2, T3, T4>(EitherFourth<T4>)
Changes a value marked for the fourth position into an either of this type.
- implicit operator Either<T1, T2, T3, T4>(EitherSecond<T2>)
Changes a value marked for the second position into an either of this type.
- implicit operator Either<T1, T2, T3, T4>(EitherThird<T3>)
Changes a value marked for the third position into an either of this type.
- operator !=(Either<T1, T2, T3, T4>, Either<T1, T2, T3, T4>)
Gives true when two instances are different. This negates operator ==(Either<T1, T2, T3, T4>, Either<T1, T2, T3, T4>).