Table of Contents

Class ResultExtensions

Namespace
Compendium.Core.Results
Assembly
Compendium.Core.dll

Extension methods for the Result types providing functional programming patterns.

public static class ResultExtensions
Inheritance
ResultExtensions
Inherited Members

Methods

BindAsync<TValue, TNewValue>(Task<Result<TValue>>, Func<TValue, Task<Result<TNewValue>>>)

Asynchronously binds the result to a new result-returning function.

public static Task<Result<TNewValue>> BindAsync<TValue, TNewValue>(this Task<Result<TValue>> resultTask, Func<TValue, Task<Result<TNewValue>>> binder)

Parameters

resultTask Task<Result<TValue>>

The result task.

binder Func<TValue, Task<Result<TNewValue>>>

The binding function.

Returns

Task<Result<TNewValue>>

A task representing the asynchronous operation with the bound result.

Type Parameters

TValue

The type of the original value.

TNewValue

The type of the new value.

Bind<TValue, TNewValue>(Result<TValue>, Func<TValue, Result<TNewValue>>)

Binds the result to a new result-returning function.

public static Result<TNewValue> Bind<TValue, TNewValue>(this Result<TValue> result, Func<TValue, Result<TNewValue>> binder)

Parameters

result Result<TValue>

The result.

binder Func<TValue, Result<TNewValue>>

The binding function.

Returns

Result<TNewValue>

The result of the binding function if successful; otherwise, the original error.

Type Parameters

TValue

The type of the original value.

TNewValue

The type of the new value.

MapAsync<TValue, TNewValue>(Task<Result<TValue>>, Func<TValue, TNewValue>)

Asynchronously maps the value of a successful result to a new value.

public static Task<Result<TNewValue>> MapAsync<TValue, TNewValue>(this Task<Result<TValue>> resultTask, Func<TValue, TNewValue> mapper)

Parameters

resultTask Task<Result<TValue>>

The result task.

mapper Func<TValue, TNewValue>

The mapping function.

Returns

Task<Result<TNewValue>>

A task representing the asynchronous operation with the mapped result.

Type Parameters

TValue

The type of the original value.

TNewValue

The type of the new value.

Map<TValue>(Result, TValue)

Converts a Result to a Result<TValue>.

public static Result<TValue> Map<TValue>(this Result result, TValue value)

Parameters

result Result

The result.

value TValue

The value to use if the result is successful.

Returns

Result<TValue>

A Result<TValue> with the specified value if successful; otherwise, the original error.

Type Parameters

TValue

The type of the value.

Map<TValue, TNewValue>(Result<TValue>, Func<TValue, TNewValue>)

Maps the value of a successful result to a new value.

public static Result<TNewValue> Map<TValue, TNewValue>(this Result<TValue> result, Func<TValue, TNewValue> mapper)

Parameters

result Result<TValue>

The result.

mapper Func<TValue, TNewValue>

The mapping function.

Returns

Result<TNewValue>

A new result with the mapped value if successful; otherwise, the original error.

Type Parameters

TValue

The type of the original value.

TNewValue

The type of the new value.

Match<TValue>(Result<TValue>, Action<TValue>, Action<Error>)

Matches the result and executes the appropriate action.

public static void Match<TValue>(this Result<TValue> result, Action<TValue> onSuccess, Action<Error> onFailure)

Parameters

result Result<TValue>

The result.

onSuccess Action<TValue>

The action to execute on success.

onFailure Action<Error>

The action to execute on failure.

Type Parameters

TValue

The type of the value.

Match<TValue, TResult>(Result<TValue>, Func<TValue, TResult>, Func<Error, TResult>)

Matches the result and executes the appropriate function.

public static TResult Match<TValue, TResult>(this Result<TValue> result, Func<TValue, TResult> onSuccess, Func<Error, TResult> onFailure)

Parameters

result Result<TValue>

The result.

onSuccess Func<TValue, TResult>

The function to execute on success.

onFailure Func<Error, TResult>

The function to execute on failure.

Returns

TResult

The result of the executed function.

Type Parameters

TValue

The type of the value.

TResult

The type of the result.

TapAsync<TValue>(Task<Result<TValue>>, Func<TValue, Task>)

Asynchronously executes a function if the result is successful.

public static Task<Result<TValue>> TapAsync<TValue>(this Task<Result<TValue>> resultTask, Func<TValue, Task> onSuccess)

Parameters

resultTask Task<Result<TValue>>

The result task.

onSuccess Func<TValue, Task>

The function to execute on success.

Returns

Task<Result<TValue>>

A task representing the asynchronous operation with the original result.

Type Parameters

TValue

The type of the value.

TapError<TValue>(Result<TValue>, Action<Error>)

Executes a function if the result is failed.

public static Result<TValue> TapError<TValue>(this Result<TValue> result, Action<Error> onFailure)

Parameters

result Result<TValue>

The result.

onFailure Action<Error>

The function to execute on failure.

Returns

Result<TValue>

The original result.

Type Parameters

TValue

The type of the value.

Tap<TValue>(Result<TValue>, Action<TValue>)

Executes a function if the result is successful.

public static Result<TValue> Tap<TValue>(this Result<TValue> result, Action<TValue> onSuccess)

Parameters

result Result<TValue>

The result.

onSuccess Action<TValue>

The function to execute on success.

Returns

Result<TValue>

The original result.

Type Parameters

TValue

The type of the value.