Table of Contents

Struct Result

Namespace
FunctionalDdd
Assembly
FunctionalDdd.RailwayOrientedProgramming.dll

Non-generic Result utility host containing factory and helper methods to construct Result<TValue> instances. NOTE: This struct is not intended to be instantiated; all members are static.

public readonly struct Result
Inherited Members
Extension Methods

Methods

Failure(Error)

Creates a failed unit result with the specified error.

public static Result<Unit> Failure(Error error)

Parameters

error Error

Error describing the failure.

Returns

Result<Unit>

A failed Result<TValue> of Unit.

FailureIfAsync<TValue>(Func<Task<bool>>, TValue, Error)

Asynchronously determines failure/success using failurePredicate (inverse semantics of SuccessIfAsync<TValue>(Func<Task<bool>>, TValue, Error)).

public static Task<Result<TValue>> FailureIfAsync<TValue>(Func<Task<bool>> failurePredicate, TValue value, Error error)

Parameters

failurePredicate Func<Task<bool>>

Async predicate producing true for failure.

value TValue

Success value if predicate is false.

error Error

Error if predicate is true.

Returns

Task<Result<TValue>>

A task producing a success or failure Result<TValue>.

Type Parameters

TValue

Type of the value.

FailureIf<TValue>(bool, TValue, Error)

Returns failure if isFailure is true; otherwise success with value.

public static Result<TValue> FailureIf<TValue>(bool isFailure, TValue value, Error error)

Parameters

isFailure bool

If true produce a failure result.

value TValue

Success value when not failing.

error Error

Error when failing.

Returns

Result<TValue>

A success or failure Result<TValue>.

Type Parameters

TValue

Type of the value.

FailureIf<TValue>(Func<bool>, in TValue, Error)

Returns failure if the provided predicate returns true; otherwise success with value.

public static Result<TValue> FailureIf<TValue>(Func<bool> failurePredicate, in TValue value, Error error)

Parameters

failurePredicate Func<bool>

Predicate indicating a failure condition.

value TValue

Success value when predicate is false.

error Error

Error when predicate is true.

Returns

Result<TValue>

A success or failure Result<TValue>.

Type Parameters

TValue

Type of the value.

Failure<TValue>(Error)

Creates a failed result with the specified error.

public static Result<TValue> Failure<TValue>(Error error)

Parameters

error Error

Error describing the failure.

Returns

Result<TValue>

A failed Result<TValue>.

Type Parameters

TValue

Type of the (missing) success value.

Failure<TValue>(Func<Error>)

Creates a failed result using a deferred error factory.

public static Result<TValue> Failure<TValue>(Func<Error> error)

Parameters

error Func<Error>

Factory function producing an Error.

Returns

Result<TValue>

A failed Result<TValue>.

Type Parameters

TValue

Type of the (missing) success value.

FromException(Exception, Func<Exception, Error>?)

Converts an exception to a failed unit result using the optional mapper (default Unexpected).

public static Result<Unit> FromException(Exception ex, Func<Exception, Error>? map = null)

Parameters

ex Exception

Exception to convert.

map Func<Exception, Error>

Optional exception-to-error mapper.

Returns

Result<Unit>

A failed unit result.

FromException<T>(Exception, Func<Exception, Error>?)

Converts an exception to a failed result of type T using the optional mapper (default Unexpected).

public static Result<T> FromException<T>(Exception ex, Func<Exception, Error>? map = null)

Parameters

ex Exception

Exception to convert.

map Func<Exception, Error>

Optional exception-to-error mapper.

Returns

Result<T>

A failed result.

Type Parameters

T

Type parameter of the target result.

Success()

Creates a successful unit result (no payload).

public static Result<Unit> Success()

Returns

Result<Unit>

A successful Result<TValue> of Unit.

SuccessIfAsync<TValue>(Func<Task<bool>>, TValue, Error)

Asynchronously determines success/failure using predicate.

public static Task<Result<TValue>> SuccessIfAsync<TValue>(Func<Task<bool>> predicate, TValue value, Error error)

Parameters

predicate Func<Task<bool>>

Async predicate producing true for success.

value TValue

Success value if predicate is true.

error Error

Error if predicate is false.

Returns

Task<Result<TValue>>

A task producing a success or failure Result<TValue>.

Type Parameters

TValue

Type of the success value.

SuccessIf<TValue>(bool, in TValue, Error)

Returns a success or failure result based on isSuccess.

public static Result<TValue> SuccessIf<TValue>(bool isSuccess, in TValue value, Error error)

Parameters

isSuccess bool

If true returns success; otherwise failure.

value TValue

Value for the success case.

error Error

Error for the failure case.

Returns

Result<TValue>

A success or failure Result<TValue>.

Type Parameters

TValue

Type of the success value.

SuccessIf<T1, T2>(bool, in T1, in T2, Error)

Returns a success (tuple) or failure result based on isSuccess.

public static Result<(T1, T2)> SuccessIf<T1, T2>(bool isSuccess, in T1 t1, in T2 t2, Error error)

Parameters

isSuccess bool

If true returns success; otherwise failure.

t1 T1

First value for the success case.

t2 T2

Second value for the success case.

error Error

Error for the failure case.

Returns

Result<(T1, T2)>

A success or failure Result<TValue> with a tuple payload.

Type Parameters

T1

Type of first value.

T2

Type of second value.

Success<TValue>(Func<TValue>)

Creates a successful result by invoking the supplied factory function.

public static Result<TValue> Success<TValue>(Func<TValue> funcOk)

Parameters

funcOk Func<TValue>

Factory function producing the value. Must not be null.

Returns

Result<TValue>

A successful Result<TValue> containing the produced value.

Type Parameters

TValue

Type of the success value.

Exceptions

ArgumentNullException

Thrown if funcOk is null.

Success<TValue>(TValue)

Creates a successful result wrapping the provided value.

public static Result<TValue> Success<TValue>(TValue value)

Parameters

value TValue

Value to wrap in a successful result (may be null for reference types).

Returns

Result<TValue>

A successful Result<TValue> containing value.

Type Parameters

TValue

Type of the success value.

TryAsync<T>(Func<Task<T>>, Func<Exception, Error>?)

Executes the asynchronous function and converts exceptions to a failed result using the optional mapper (default maps to Unexpected).

public static Task<Result<T>> TryAsync<T>(Func<Task<T>> func, Func<Exception, Error>? map = null)

Parameters

func Func<Task<T>>

Asynchronous function to execute.

map Func<Exception, Error>

Optional exception-to-error mapper. If null, a default Unexpected error is used.

Returns

Task<Result<T>>

A task producing either a success or failure result.

Type Parameters

T

Type of the produced value.

Try<T>(Func<T>, Func<Exception, Error>?)

Executes the function and converts exceptions to a failed result using the optional mapper (default maps to Unexpected(string, string, string?)).

public static Result<T> Try<T>(Func<T> func, Func<Exception, Error>? map = null)

Parameters

func Func<T>

Function to execute.

map Func<Exception, Error>

Optional exception-to-error mapper. If null, a default Unexpected error is used.

Returns

Result<T>

A success result with the value or a failure result if an exception was thrown.

Type Parameters

T

Type of the produced value.