Table of Contents

Struct Maybe<T>

Namespace
SodaFlow.Functional
Assembly
SodaFlow.Functional.dll

A value that is there, or that is not there.

[PublicAPI]
public struct Maybe<T> : IMaybe, IEquatable<Maybe<T>>, IComparable<Maybe<T>>

Type Parameters

T

The type of the value, when there is one.

Implements
Inherited Members
Extension Methods

Remarks

Unlike Nullable<T> this works for reference types and for value types. Unlike a null reference, the type itself shows that there can be no value. There is no property that hands the value out unchecked. Read the value with Match<TResult>(Func<T, TResult>, Func<TResult>), or with one of the helpers on it, thus the caller must answer for the condition with no value. This is a struct, so default is a correct instance and holds nothing: the same as None.

Fields

None

A Maybe<T> containing no value.

Methods

CompareTo(Maybe<T>)

Puts this instance in order against a second one, and a missing value comes first.

Equals(Maybe<T>)

Gives true when the given instance is equal to this one.

Equals(object?)

Gives true when the given object is a Maybe<T> of this type equal to this one.

GetHashCode()

Gives a hash code that agrees with operator ==(Maybe<T>, Maybe<T>).

HasValue()

Gives true when there is a value.

Map<TResult>(Func<T, TResult>)

Maps the value with the given function when there is one, and gives no value when there is none.

MatchAsyncVoid(Func<T, Task>, Func<Task>)

Runs one asynchronous action when there is a value, and a different one when there is none.

MatchAsync<TResult>(Func<T, Task<TResult>>, Func<Task<TResult>>)

Runs one asynchronous function when there is a value, and a different one when there is none, and gives the result.

MatchNone(Action)

Runs an action when there is no value, and otherwise does nothing.

MatchNoneAsync(Func<Task>)

Runs an asynchronous action when there is no value, and otherwise does nothing.

MatchSome(Action<T>)

Runs an action with the contained value when there is one, and otherwise does nothing.

MatchSomeAsync(Func<T, Task>)

Runs an asynchronous action with the contained value when there is one, and otherwise does nothing.

MatchVoid(Action<T>, Action)

Runs one action when there is a value, and a different action when there is none.

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

Runs one function when there is a value, and a different one when there is none.

Some(T)

Creates a Maybe<T> containing the given value.

ToString()

Returns a readable description of this instance, for diagnostics.

Operators

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

Gives true when two instances contain equal values, or the two contain none.

implicit operator Maybe<T>(NoneType)

Changes the untyped None into a Maybe<T> of this type containing no value.

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

Gives true when two instances are different. This negates operator ==(Maybe<T>, Maybe<T>).