Table of Contents

Struct Either<T1, T2>

Namespace
LfrlAnvil.Functional
Assembly
LfrlAnvil.Functional.dll

Represents a generic pair of exclusive values.

public readonly struct Either<T1, T2> : IEquatable<Either<T1, T2>>

Type Parameters

T1

First value type.

T2

Second value type.

Implements
Inherited Members
Extension Methods

Fields

Empty

Represents an empty either, with default second value.

public static readonly Either<T1, T2> Empty

Field Value

Either<T1, T2>

Properties

HasFirst

Specifies whether or not this instance contains first value.

public bool HasFirst { get; }

Property Value

bool

HasSecond

Specifies whether or not this instance contains second value.

public bool HasSecond { get; }

Property Value

bool

Methods

BindSecond<T3>(Func<T2, Either<T1, T3>>)

Returns the result of the provided second delegate invocation when HasSecond is equal to true, otherwise returns this instance.

[Pure]
public Either<T1, T3> BindSecond<T3>(Func<T2, Either<T1, T3>> second)

Parameters

second Func<T2, Either<T1, T3>>

Delegate to invoke when HasSecond is equal to true.

Returns

Either<T1, T3>

New Either<T1, T2> instance.

Type Parameters

T3

Result type.

Bind<T3>(Func<T1, Either<T3, T2>>)

Returns the result of the provided first delegate invocation when HasFirst is equal to true, otherwise returns this instance.

[Pure]
public Either<T3, T2> Bind<T3>(Func<T1, Either<T3, T2>> first)

Parameters

first Func<T1, Either<T3, T2>>

Delegate to invoke when HasFirst is equal to true.

Returns

Either<T3, T2>

New Either<T1, T2> instance.

Type Parameters

T3

Result type.

Bind<T3, T4>(Func<T1, Either<T3, T4>>, Func<T2, Either<T3, T4>>)

Returns the result of the provided first delegate invocation when HasFirst is equal to true, otherwise returns the result of the provided second delegate invocation.

[Pure]
public Either<T3, T4> Bind<T3, T4>(Func<T1, Either<T3, T4>> first, Func<T2, Either<T3, T4>> second)

Parameters

first Func<T1, Either<T3, T4>>

Delegate to invoke when HasFirst is equal to true.

second Func<T2, Either<T3, T4>>

Delegate to invoke when HasFirst is equal to false.

Returns

Either<T3, T4>

New Either<T1, T2> instance.

Type Parameters

T3

First delegate's result type.

T4

Second delegate's result type.

Equals(Either<T1, T2>)

Indicates whether the current object is equal to another object of the same type.

[Pure]
public bool Equals(Either<T1, T2> other)

Parameters

other Either<T1, T2>

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

[Pure]
public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetFirst()

Gets the first value.

[Pure]
public T1 GetFirst()

Returns

T1

First value.

Exceptions

ValueAccessException

When first value does not exist.

GetFirstOrDefault()

Gets the first value or a default value when it does not exist.

[Pure]
public T1? GetFirstOrDefault()

Returns

T1

First value or a default value when it does not exist.

GetFirstOrDefault(T1)

Gets the first value or a default value when it does not exist.

[Pure]
public T1 GetFirstOrDefault(T1 defaultValue)

Parameters

defaultValue T1

Default value to return in case the first value does not exist.

Returns

T1

First value or a default value when it does not exist.

GetHashCode()

Returns the hash code for this instance.

[Pure]
public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

GetSecond()

Gets the second value.

[Pure]
public T2 GetSecond()

Returns

T2

Second value.

Exceptions

ValueAccessException

When second value does not exist.

GetSecondOrDefault()

Gets the second value or a default value when it does not exist.

[Pure]
public T2? GetSecondOrDefault()

Returns

T2

Second value or a default value when it does not exist.

GetSecondOrDefault(T2)

Gets the second value or a default value when it does not exist.

[Pure]
public T2 GetSecondOrDefault(T2 defaultValue)

Parameters

defaultValue T2

Default value to return in case the second value does not exist.

Returns

T2

Second value or a default value when it does not exist.

IfFirst(Action<T1>)

Invokes the provided first delegate when HasFirst is equal to true, otherwise does nothing.

public Nil IfFirst(Action<T1> first)

Parameters

first Action<T1>

Delegate to invoke when HasFirst is equal to true.

Returns

Nil

Nil.

IfFirstOrDefault<T3>(Func<T1, T3>)

Returns the result of the provided first delegate invocation when HasFirst is equal to true, otherwise returns a default value.

[Pure]
public T3? IfFirstOrDefault<T3>(Func<T1, T3> first)

Parameters

first Func<T1, T3>

Delegate to invoke when HasFirst is equal to true.

Returns

T3

Delegate invocation result or a default value.

Type Parameters

T3

Result type.

IfFirstOrDefault<T3>(Func<T1, T3>, T3)

Returns the result of the provided first delegate invocation when HasFirst is equal to true, otherwise returns a default value.

[Pure]
public T3 IfFirstOrDefault<T3>(Func<T1, T3> first, T3 defaultValue)

Parameters

first Func<T1, T3>

Delegate to invoke when HasFirst is equal to true.

defaultValue T3

Value to return HasFirst is equal to false.

Returns

T3

Delegate invocation result or a default value.

Type Parameters

T3

Result type.

IfFirst<T3>(Func<T1, T3?>)

Returns the result of the provided first delegate invocation when HasFirst is equal to true, otherwise returns None.

[Pure]
public Maybe<T3> IfFirst<T3>(Func<T1, T3?> first) where T3 : notnull

Parameters

first Func<T1, T3>

Delegate to invoke when HasFirst is equal to true.

Returns

Maybe<T3>

New Maybe<T> instance.

Type Parameters

T3

Result type.

IfSecond(Action<T2>)

Invokes the provided second delegate when HasSecond is equal to true, otherwise does nothing.

public Nil IfSecond(Action<T2> second)

Parameters

second Action<T2>

Delegate to invoke when HasSecond is equal to true.

Returns

Nil

Nil.

IfSecondOrDefault<T3>(Func<T2, T3>)

Returns the result of the provided second delegate invocation when HasSecond is equal to true, otherwise returns a default value.

[Pure]
public T3? IfSecondOrDefault<T3>(Func<T2, T3> second)

Parameters

second Func<T2, T3>

Delegate to invoke when HasSecond is equal to true.

Returns

T3

Delegate invocation result or a default value.

Type Parameters

T3

Result type.

IfSecondOrDefault<T3>(Func<T2, T3>, T3)

Returns the result of the provided second delegate invocation when HasSecond is equal to true, otherwise returns a default value.

[Pure]
public T3 IfSecondOrDefault<T3>(Func<T2, T3> second, T3 defaultValue)

Parameters

second Func<T2, T3>

Delegate to invoke when HasSecond is equal to true.

defaultValue T3

Value to return HasSecond is equal to false.

Returns

T3

Delegate invocation result or a default value.

Type Parameters

T3

Result type.

IfSecond<T3>(Func<T2, T3?>)

Returns the result of the provided second delegate invocation when HasSecond is equal to true, otherwise returns None.

[Pure]
public Maybe<T3> IfSecond<T3>(Func<T2, T3?> second) where T3 : notnull

Parameters

second Func<T2, T3>

Delegate to invoke when HasSecond is equal to true.

Returns

Maybe<T3>

New Maybe<T> instance.

Type Parameters

T3

Result type.

Match(Action<T1>, Action<T2>)

Invokes the provided first delegate when HasFirst is equal to true, otherwise invokes the provided second delegate.

public Nil Match(Action<T1> first, Action<T2> second)

Parameters

first Action<T1>

Delegate to invoke when HasFirst is equal to true.

second Action<T2>

Delegate to invoke when HasFirst is equal to false.

Returns

Nil

Nil.

Match<T3>(Func<T1, T3>, Func<T2, T3>)

Returns the result of the provided first delegate invocation when HasFirst is equal to true, otherwise returns the result of the provided second delegate invocation.

[Pure]
public T3 Match<T3>(Func<T1, T3> first, Func<T2, T3> second)

Parameters

first Func<T1, T3>

Delegate to invoke when HasFirst is equal to true.

second Func<T2, T3>

Delegate to invoke when HasFirst is equal to false.

Returns

T3

Delegate invocation result.

Type Parameters

T3

Result type.

Swap()

Creates a new Either<T1, T2> instance by swapping first and second positions.

[Pure]
public Either<T2, T1> Swap()

Returns

Either<T2, T1>

New Either<T1, T2> instance.

ToString()

Returns a string representation of this Either<T1, T2> instance.

[Pure]
public override string ToString()

Returns

string

String representation.

Operators

operator ==(Either<T1, T2>, Either<T1, T2>)

Checks if a is equal to b.

[Pure]
public static bool operator ==(Either<T1, T2> a, Either<T1, T2> b)

Parameters

a Either<T1, T2>

First operand.

b Either<T1, T2>

Second operand.

Returns

bool

true when operands are equal, otherwise false.

explicit operator T1(Either<T1, T2>)

Gets the first value.

[Pure]
public static explicit operator T1(Either<T1, T2> value)

Parameters

value Either<T1, T2>

Returns

T1

First value.

Exceptions

ValueAccessException

When first value does not exist.

explicit operator T2(Either<T1, T2>)

Gets the second value.

[Pure]
public static explicit operator T2(Either<T1, T2> value)

Parameters

value Either<T1, T2>

Returns

T2

Second value.

Exceptions

ValueAccessException

When second value does not exist.

implicit operator Either<T1, T2>(Nil)

Converts Nil to an Either<T1, T2> instance.

[Pure]
public static implicit operator Either<T1, T2>(Nil value)

Parameters

value Nil

Value to convert.

Returns

Either<T1, T2>

Empty.

implicit operator Either<T1, T2>(PartialEither<T1>)

Converts the provided part to an Either<T1, T2> instance with a first value.

[Pure]
public static implicit operator Either<T1, T2>(PartialEither<T1> part)

Parameters

part PartialEither<T1>

Value to convert.

Returns

Either<T1, T2>

New Either<T1, T2> instance.

implicit operator Either<T1, T2>(PartialEither<T2>)

Converts the provided part to an Either<T1, T2> instance with a second value.

[Pure]
public static implicit operator Either<T1, T2>(PartialEither<T2> part)

Parameters

part PartialEither<T2>

Value to convert.

Returns

Either<T1, T2>

New Either<T1, T2> instance.

implicit operator Either<T1, T2>(T1)

Converts the provided first to an Either<T1, T2> instance with a first value.

[Pure]
public static implicit operator Either<T1, T2>(T1 first)

Parameters

first T1

Value to convert.

Returns

Either<T1, T2>

New Either<T1, T2> instance.

implicit operator Either<T1, T2>(T2)

Converts the provided second to an Either<T1, T2> instance with a second value.

[Pure]
public static implicit operator Either<T1, T2>(T2 second)

Parameters

second T2

Value to convert.

Returns

Either<T1, T2>

New Either<T1, T2> instance.

operator !=(Either<T1, T2>, Either<T1, T2>)

Checks if a is not equal to b.

[Pure]
public static bool operator !=(Either<T1, T2> a, Either<T1, T2> b)

Parameters

a Either<T1, T2>

First operand.

b Either<T1, T2>

Second operand.

Returns

bool

true when operands are not equal, otherwise false.