Table of Contents

Struct Maybe<T>

Namespace
LfrlAnvil.Functional
Assembly
LfrlAnvil.Functional.dll

Represents a generic optional value.

public readonly struct Maybe<T> : IEquatable<Maybe<T>>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable where T : notnull

Type Parameters

T

Value type.

Implements
Inherited Members
Extension Methods

Fields

None

Represents a lack of value.

public static readonly Maybe<T> None

Field Value

Maybe<T>

Properties

HasValue

Specifies whether or not this instance contains a non-null value.

public bool HasValue { get; }

Property Value

bool

Methods

Bind<T2>(Func<T, Maybe<T2>>)

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

[Pure]
public Maybe<T2> Bind<T2>(Func<T, Maybe<T2>> some) where T2 : notnull

Parameters

some Func<T, Maybe<T2>>

Delegate to invoke when HasValue is equal to true.

Returns

Maybe<T2>

New Maybe<T> instance.

Type Parameters

T2

Result type.

Bind<T2>(Func<T, Maybe<T2>>, Func<Maybe<T2>>)

Returns the result of the provided some delegate invocation when HasValue is equal to true, otherwise returns the result of the provided none delegate invocation.

[Pure]
public Maybe<T2> Bind<T2>(Func<T, Maybe<T2>> some, Func<Maybe<T2>> none) where T2 : notnull

Parameters

some Func<T, Maybe<T2>>

Delegate to invoke when HasValue is equal to true.

none Func<Maybe<T2>>

Delegate to invoke when HasValue is equal to false.

Returns

Maybe<T2>

New Maybe<T> instance.

Type Parameters

T2

Result type.

Equals(Maybe<T>)

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

[Pure]
public bool Equals(Maybe<T> other)

Parameters

other Maybe<T>

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.

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.

GetValue()

Gets the underlying value.

[Pure]
public T GetValue()

Returns

T

Underlying value.

Exceptions

ValueAccessException

When underlying value does not exist.

GetValueOrDefault()

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

[Pure]
public T? GetValueOrDefault()

Returns

T

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

GetValueOrDefault(T)

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

[Pure]
public T GetValueOrDefault(T defaultValue)

Parameters

defaultValue T

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

Returns

T

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

IfNone(Action)

Invokes the provided none delegate when HasValue is equal to false, otherwise does nothing.

public Nil IfNone(Action none)

Parameters

none Action

Delegate to invoke when HasValue is equal to false.

Returns

Nil

Nil.

IfNoneOrDefault<T2>(Func<T2>)

Returns the result of the provided none delegate invocation when HasValue is equal to false, otherwise returns a default value.

[Pure]
public T2? IfNoneOrDefault<T2>(Func<T2> none)

Parameters

none Func<T2>

Delegate to invoke when HasValue is equal to false.

Returns

T2

Delegate invocation result or a default value.

Type Parameters

T2

Result type.

IfNoneOrDefault<T2>(Func<T2>, T2)

Returns the result of the provided none delegate invocation when HasValue is equal to false, otherwise returns a default value.

[Pure]
public T2 IfNoneOrDefault<T2>(Func<T2> none, T2 defaultValue)

Parameters

none Func<T2>

Delegate to invoke when HasValue is equal to false.

defaultValue T2

Value to return HasValue is equal to true.

Returns

T2

Delegate invocation result or a default value.

Type Parameters

T2

Result type.

IfNone<T2>(Func<T2?>)

Returns the result of the provided none delegate invocation when HasValue is equal to false, otherwise returns None.

[Pure]
public Maybe<T2> IfNone<T2>(Func<T2?> none) where T2 : notnull

Parameters

none Func<T2>

Delegate to invoke when HasValue is equal to false.

Returns

Maybe<T2>

New Maybe<T> instance.

Type Parameters

T2

Result type.

IfSome(Action<T>)

Invokes the provided some delegate when HasValue is equal to true, otherwise does nothing.

public Nil IfSome(Action<T> some)

Parameters

some Action<T>

Delegate to invoke when HasValue is equal to true.

Returns

Nil

Nil.

IfSomeOrDefault<T2>(Func<T, T2>)

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

[Pure]
public T2? IfSomeOrDefault<T2>(Func<T, T2> some)

Parameters

some Func<T, T2>

Delegate to invoke when HasValue is equal to true.

Returns

T2

Delegate invocation result or a default value.

Type Parameters

T2

Result type.

IfSomeOrDefault<T2>(Func<T, T2>, T2)

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

[Pure]
public T2 IfSomeOrDefault<T2>(Func<T, T2> some, T2 defaultValue)

Parameters

some Func<T, T2>

Delegate to invoke when HasValue is equal to true.

defaultValue T2

Value to return HasValue is equal to false.

Returns

T2

Delegate invocation result or a default value.

Type Parameters

T2

Result type.

IfSome<T2>(Func<T, T2?>)

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

[Pure]
public Maybe<T2> IfSome<T2>(Func<T, T2?> some) where T2 : notnull

Parameters

some Func<T, T2>

Delegate to invoke when HasValue is equal to true.

Returns

Maybe<T2>

New Maybe<T> instance.

Type Parameters

T2

Result type.

Match(Action<T>, Action)

Invokes the provided some delegate when HasValue is equal to true, otherwise invokes the provided none delegate.

public Nil Match(Action<T> some, Action none)

Parameters

some Action<T>

Delegate to invoke when HasValue is equal to true.

none Action

Delegate to invoke when HasValue is equal to false.

Returns

Nil

Nil.

Match<T2>(Func<T, T2>, Func<T2>)

Returns the result of the provided some delegate invocation when HasValue is equal to true, otherwise returns the result of the provided none delegate invocation.

[Pure]
public T2 Match<T2>(Func<T, T2> some, Func<T2> none)

Parameters

some Func<T, T2>

Delegate to invoke when HasValue is equal to true.

none Func<T2>

Delegate to invoke when HasValue is equal to false.

Returns

T2

Delegate invocation result.

Type Parameters

T2

Result type.

ToString()

Returns a string representation of this Maybe<T> instance.

[Pure]
public override string ToString()

Returns

string

String representation.

Operators

operator ==(Maybe<T>, Maybe<T>)

Checks if a is equal to b.

[Pure]
public static bool operator ==(Maybe<T> a, Maybe<T> b)

Parameters

a Maybe<T>

First operand.

b Maybe<T>

Second operand.

Returns

bool

true when operands are equal, otherwise false.

explicit operator T(Maybe<T>)

Gets the underlying value.

[Pure]
public static explicit operator T(Maybe<T> value)

Parameters

value Maybe<T>

Returns

T

Underlying value.

Exceptions

ValueAccessException

When underlying value does not exist.

implicit operator Maybe<T>(Nil)

Converts Nil to a Maybe<T> instance.

[Pure]
public static implicit operator Maybe<T>(Nil none)

Parameters

none Nil

Value to convert.

Returns

Maybe<T>

None.

implicit operator Maybe<T>(T?)

Converts the provided value to a Maybe<T> instance.

[Pure]
public static implicit operator Maybe<T>(T? value)

Parameters

value T

Value to convert.

Returns

Maybe<T>

New Maybe<T> instance.

operator !=(Maybe<T>, Maybe<T>)

Checks if a is not equal to b.

[Pure]
public static bool operator !=(Maybe<T> a, Maybe<T> b)

Parameters

a Maybe<T>

First operand.

b Maybe<T>

Second operand.

Returns

bool

true when operands are not equal, otherwise false.