Table of Contents

OptionAsyncExtensions Class

Definition

Namespace
Bodu.Functional
Assembly
Bodu.Core.dll
Package
Bodu.Core 1.0.1
Source
OptionAsyncExtensions.BindAsync.cs

Provides Task-based asynchronous companions to the Option<T> railway combinators.

public static class OptionAsyncExtensions
Inheritance
OptionAsyncExtensions
Inherited Members

Remarks

Each operation mirrors its synchronous counterpart - MapAsync pairs with Map<TResult>(Func<T, TResult>), BindAsync with Bind<TResult>(Func<T, Option<TResult>>), and MatchAsync with Match<TResult>(Func<T, TResult>, Func<TResult>) - so an asynchronous pipeline over Task<Option<T>> composes exactly like its synchronous equivalent.

Argument validation runs synchronously, so a null source task or delegate faults at the call site rather than on the returned task.

The combinators accept no CancellationToken: they perform no I/O of their own, so cancellation belongs to the caller's delegates, which can close over their own tokens.

Methods

BindAsync<T, TResult>(Option<T>, Func<T, Task<Option<TResult>>>)

Asynchronously projects the contained value into another option using the specified asynchronous option-returning selector.

public static Task<Option<TResult>> BindAsync<T, TResult>(this Option<T> source, Func<T, Task<Option<TResult>>> selector)

Parameters

source Option<T>

The option to project.

selector Func<T, Task<Option<TResult>>>

The asynchronous option-returning projection applied to the contained value.

Returns

Task<Option<TResult>>

A task that completes with the option produced by the awaited selector when the option carries a value; otherwise None.

Type Parameters

T

The value type of the source option.

TResult

The value type of the resulting option.

Remarks

The selector is not invoked when the option is None. Argument validation runs synchronously, so a null selector faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

selector is null.

BindAsync<T, TResult>(Task<Option<T>>, Func<T, Option<TResult>>)

Asynchronously projects the value contained by the awaited option into another option using the specified option-returning selector.

public static Task<Option<TResult>> BindAsync<T, TResult>(this Task<Option<T>> source, Func<T, Option<TResult>> selector)

Parameters

source Task<Option<T>>

The task producing the option to project. Must not be null.

selector Func<T, Option<TResult>>

The option-returning projection applied to the contained value.

Returns

Task<Option<TResult>>

A task that completes with the option produced by the selector when the awaited option carries a value; otherwise None.

Type Parameters

T

The value type of the source option.

TResult

The value type of the resulting option.

Remarks

The selector is not invoked when the awaited option is None. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source or selector is null.

BindAsync<T, TResult>(Task<Option<T>>, Func<T, Task<Option<TResult>>>)

Asynchronously projects the value contained by the awaited option into another option using the specified asynchronous option-returning selector.

public static Task<Option<TResult>> BindAsync<T, TResult>(this Task<Option<T>> source, Func<T, Task<Option<TResult>>> selector)

Parameters

source Task<Option<T>>

The task producing the option to project. Must not be null.

selector Func<T, Task<Option<TResult>>>

The asynchronous option-returning projection applied to the contained value.

Returns

Task<Option<TResult>>

A task that completes with the option produced by the awaited selector when the awaited option carries a value; otherwise None.

Type Parameters

T

The value type of the source option.

TResult

The value type of the resulting option.

Remarks

The selector is not invoked when the awaited option is None. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source or selector is null.

MapAsync<T, TResult>(Option<T>, Func<T, Task<TResult>>)

Asynchronously projects the contained value using the specified asynchronous selector.

public static Task<Option<TResult>> MapAsync<T, TResult>(this Option<T> source, Func<T, Task<TResult>> selector)

Parameters

source Option<T>

The option to project.

selector Func<T, Task<TResult>>

The asynchronous projection applied to the contained value.

Returns

Task<Option<TResult>>

A task that completes with Some(await selector(value)) when the option carries a value and the awaited projection is non-null; otherwise None.

Type Parameters

T

The value type of the source option.

TResult

The type produced by the selector.

Remarks

The selector is not invoked when the option is None. A null awaited projection result maps to None (the lenient lift), matching Map<TResult>(Func<T, TResult>). Argument validation runs synchronously, so a null selector faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

selector is null.

MapAsync<T, TResult>(Task<Option<T>>, Func<T, Task<TResult>>)

Asynchronously projects the value contained by the awaited option using the specified asynchronous selector.

public static Task<Option<TResult>> MapAsync<T, TResult>(this Task<Option<T>> source, Func<T, Task<TResult>> selector)

Parameters

source Task<Option<T>>

The task producing the option to project. Must not be null.

selector Func<T, Task<TResult>>

The asynchronous projection applied to the contained value.

Returns

Task<Option<TResult>>

A task that completes with Some(await selector(value)) when the awaited option carries a value and the awaited projection is non-null; otherwise None.

Type Parameters

T

The value type of the source option.

TResult

The type produced by the selector.

Remarks

The selector is not invoked when the awaited option is None. A null awaited projection result maps to None (the lenient lift), matching Map<TResult>(Func<T, TResult>). Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source or selector is null.

MapAsync<T, TResult>(Task<Option<T>>, Func<T, TResult>)

Asynchronously projects the value contained by the awaited option using the specified selector.

public static Task<Option<TResult>> MapAsync<T, TResult>(this Task<Option<T>> source, Func<T, TResult> selector)

Parameters

source Task<Option<T>>

The task producing the option to project. Must not be null.

selector Func<T, TResult>

The projection applied to the contained value.

Returns

Task<Option<TResult>>

A task that completes with Some(selector(value)) when the awaited option carries a value and the projection is non-null; otherwise None.

Type Parameters

T

The value type of the source option.

TResult

The type produced by the selector.

Remarks

The selector is not invoked when the awaited option is None. A null projection result maps to None (the lenient lift), matching Map<TResult>(Func<T, TResult>). Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source or selector is null.

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

Asynchronously collapses the option to a single value by invoking and awaiting the matching asynchronous branch.

public static Task<TResult> MatchAsync<T, TResult>(this Option<T> source, Func<T, Task<TResult>> onSome, Func<Task<TResult>> onNone)

Parameters

source Option<T>

The option to collapse.

onSome Func<T, Task<TResult>>

The asynchronous projection invoked with the contained value when one is present.

onNone Func<Task<TResult>>

The asynchronous factory invoked when no value is present.

Returns

Task<TResult>

A task that completes with the value produced by the awaited branch.

Type Parameters

T

The value type of the source option.

TResult

The type produced by both branches.

Remarks

Exactly one branch is invoked. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

onSome or onNone is null.

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

Asynchronously collapses the awaited option to a single value by invoking and awaiting the matching asynchronous branch.

public static Task<TResult> MatchAsync<T, TResult>(this Task<Option<T>> source, Func<T, Task<TResult>> onSome, Func<Task<TResult>> onNone)

Parameters

source Task<Option<T>>

The task producing the option to collapse. Must not be null.

onSome Func<T, Task<TResult>>

The asynchronous projection invoked with the contained value when one is present.

onNone Func<Task<TResult>>

The asynchronous factory invoked when no value is present.

Returns

Task<TResult>

A task that completes with the value produced by the awaited branch.

Type Parameters

T

The value type of the source option.

TResult

The type produced by both branches.

Remarks

Exactly one branch is invoked. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source, onSome, or onNone is null.

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

Asynchronously collapses the awaited option to a single value by invoking the matching branch.

public static Task<TResult> MatchAsync<T, TResult>(this Task<Option<T>> source, Func<T, TResult> onSome, Func<TResult> onNone)

Parameters

source Task<Option<T>>

The task producing the option to collapse. Must not be null.

onSome Func<T, TResult>

The projection invoked with the contained value when one is present.

onNone Func<TResult>

The factory invoked when no value is present.

Returns

Task<TResult>

A task that completes with the value produced by the invoked branch.

Type Parameters

T

The value type of the source option.

TResult

The type produced by both branches.

Remarks

Exactly one branch is invoked. Argument validation runs synchronously, so a null argument faults at the call site rather than on the returned task.

Exceptions

ArgumentNullException

source, onSome, or onNone is null.

Applies to

ProductVersions
.NET8, 10