Skip to main content

Maybe API Reference

The full API surface for Maybe<T> (UnambitiousFx.Functional). T is always constrained to notnull. Synchronous members live on Maybe<T> itself or in MaybeExtensions; asynchronous members extend ValueTask<Maybe<T>> and are listed under Async support.

Creation

MethodSignatureDescription
Maybe.SomeMaybe<T> Maybe.Some<T>(T value)Wraps a non-null value as Some.
Maybe.NoneMaybe<T> Maybe.None<T>()Creates an empty None.
Maybe<T>.Somestatic Maybe<T> Some(T value)Instance-type factory for Some.
Maybe<T>.Nonestatic Maybe<T> None()Instance-type factory for None.
implicit operatorimplicit operator Maybe<T>(T? value)Converts a value to Some, or null to None.

Inspection

MemberSignatureDescription
IsSomebool IsSome { get; }true when a value is present.
IsNonebool IsNone { get; }true when no value is present.
CaseT? Case { get; }The value when Some, otherwise default(T).
Somebool Some(out T? value)Try-pattern: returns true and sets value (non-null) when Some.

Matching

MemberSignatureDescription
MatchTOut Match<TOut>(Func<T, TOut> some, Func<TOut> none)Collapses both branches to a single value.
Matchvoid Match(Action<T> some, Action none)Runs an action per branch.
Switchvoid Switch(Action<T> some, Action none)Alias of the void Match; runs an action per branch.
IfSomevoid IfSome(Action<T> some)Runs the action only when Some.
IfNonevoid IfNone(Action none)Runs the action only when None.

Transformation

MethodSignatureDescription
MapMaybe<TOut> Map<TIn, TOut>(this Maybe<TIn>, Func<TIn, TOut>)Transforms the value, preserving None.
BindMaybe<TOut> Bind<TIn, TOut>(this Maybe<TIn>, Func<TIn, Maybe<TOut>>)Transforms with a Maybe-returning function, flattening the result.
FilterMaybe<T> Filter<T>(this Maybe<T>, Func<T, bool>)Keeps the value when the predicate holds; otherwise None.

Extraction

MethodSignatureDescription
ValueOrT ValueOr<T>(this Maybe<T>, T fallback)Returns the value, or the fallback when None.
ValueOrT ValueOr<T>(this Maybe<T>, Func<T> fallbackFactory)Returns the value, or a lazily-created fallback when None.
OrElseMaybe<T> OrElse<T>(this Maybe<T>, Maybe<T> fallback)Returns this when Some; otherwise the fallback Maybe.
OrElseMaybe<T> OrElse<T>(this Maybe<T>, Func<Maybe<T>> fallbackFactory)Returns this when Some; otherwise a lazily-created fallback Maybe.

To read the raw inner value or default(T), use the Case property — there is no ValueOrDefault on Maybe<T>.

Bridging

MethodSignatureDescription
ToResultResult<T> ToResult(Failure failure)Some becomes Success; None becomes Failure with the given failure.
ToResultResult<T> ToResult(Func<Failure> errorFactory)Some becomes Success; None becomes Failure from the factory.
ToResultResult<T> ToResult(string message)Some becomes Success; None becomes Failure with the given message.

Side effects

MethodSignatureDescription
TapMaybe<T> Tap<T>(this Maybe<T>, Action<T>)Runs a side effect when Some; returns the original Maybe.
TapSomeMaybe<T> TapSome(Action<T>)Runs a side effect when Some; returns the original Maybe.
TapNoneMaybe<T> TapNone(Action)Runs a side effect when None; returns the original Maybe.

LINQ

MethodSignatureDescription
SelectMaybe<TOut> Select<TIn, TOut>(this Maybe<TIn>, Func<TIn, TOut>)LINQ projection; delegates to Map.
SelectManyMaybe<TOut> SelectMany<TIn, TOut>(this Maybe<TIn>, Func<TIn, Maybe<TOut>>)LINQ bind; delegates to Bind.
SelectManyMaybe<TOut> SelectMany<TIn, TCollection, TOut>(this Maybe<TIn>, Func<TIn, Maybe<TCollection>>, Func<TIn, TCollection, TOut>)LINQ bind with a projection; enables multi-from queries.
WhereMaybe<T> Where<T>(this Maybe<T>, Func<T, bool>)LINQ filter; delegates to Filter.

Async support

Each method below extends ValueTask<Maybe<T>> and returns a ValueTask<…>, so chains can be awaited end to end. Where applicable, both sync-delegate and async-delegate (Func<…, ValueTask<…>>) overloads exist.

MethodSignaturesDescription
MapMap(Func<T, TOut>), Map(Func<T, ValueTask<TOut>>)Async map, preserving None.
BindBind(Func<T, Maybe<TOut>>), Bind(Func<T, ValueTask<Maybe<TOut>>>)Async bind, flattening the result.
FilterFilter(Func<T, bool>), Filter(Func<T, ValueTask<bool>>)Async filter.
OrElseOrElse(Maybe<T>), OrElse(Func<Maybe<T>>), OrElse(Func<ValueTask<Maybe<T>>>)Async fallback Maybe.
ValueOrValueOr(T), ValueOr(Func<T>), ValueOr(Func<ValueTask<T>>)Async unwrap with fallback.
MatchMatch(Func<T, TOut>, Func<TOut>), Match(Func<T, ValueTask<TOut>>, Func<ValueTask<TOut>>)Async match.
SwitchSwitch(Action<T>, Action), Switch(Func<T, ValueTask>, Func<ValueTask>)Async per-branch side effect.
TapSomeTapSome(Action<T>), TapSome(Func<T, ValueTask>)Async side effect when Some.
TapNoneTapNone(Action), TapNone(Func<ValueTask>)Async side effect when None.
ToResultToResult(Failure), ToResult(Func<Failure>), ToResult(string), ToResult(Func<ValueTask<Failure>>)Async bridge to Result<T>.

See also