Table of Contents

Class BindExtensions

Namespace
Trellis
Assembly
Trellis.Core.dll

Provides extension methods for binding (chaining) operations over Result values.

public static class BindExtensions
Inheritance
BindExtensions
Inherited Members

Remarks

Bind is the core operation in Railway Oriented Programming. It allows you to chain together operations that can fail, automatically short-circuiting on the first failure. If the input Result is a success, the bind function is called with the value. If the input Result is a failure, the bind function is skipped and the failure is propagated.

Methods

Bind<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)>, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Result<TResult>>)

Chains an operation that returns a Result based on a 9-element tuple value. If the current result is a failure, returns the failure without calling the function.

public static Result<TResult> Bind<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>(this Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> result, Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Result<TResult>> func)

Parameters

result Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)>

The result containing a 9-element tuple.

func Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Result<TResult>>

The function to execute on the tuple elements if the result is successful.

Returns

Result<TResult>

A new Result containing the function's result, or the original failure.

Type Parameters

T1

The type of element 1 in the tuple.

T2

The type of element 2 in the tuple.

T3

The type of element 3 in the tuple.

T4

The type of element 4 in the tuple.

T5

The type of element 5 in the tuple.

T6

The type of element 6 in the tuple.

T7

The type of element 7 in the tuple.

T8

The type of element 8 in the tuple.

T9

The type of element 9 in the tuple.

TResult

The type of the result value returned by the function.

Examples

var result = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))
    .Combine(Field3.TryCreate("value3"))
    .Combine(Field4.TryCreate("value4"))
    .Combine(Field5.TryCreate("value5"))
    .Combine(Field6.TryCreate("value6"))
    .Combine(Field7.TryCreate("value7"))
    .Combine(Field8.TryCreate("value8"))
    .Combine(Field9.TryCreate("value9"))
    .Bind((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9) => CreateEntity(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9));

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

Binds the result to a function that returns a new result. If the starting result is a success, the function is called with the value. If the starting result is a failure, the function is skipped and the failure is returned.

public static Result<TResult> Bind<TValue, TResult>(this Result<TValue> result, Func<TValue, Result<TResult>> func)

Parameters

result Result<TValue>

The result to bind.

func Func<TValue, Result<TResult>>

The function to call if the result is successful.

Returns

Result<TResult>

A new result from the function if success; otherwise the original failure.

Type Parameters

TValue

Type of the input result value.

TResult

Type of the output result value.

Bind<T1, T2, TResult>(Result<(T1, T2)>, Func<T1, T2, Result<TResult>>)

Chains an operation that returns a Result based on a 2-element tuple value. If the current result is a failure, returns the failure without calling the function.

public static Result<TResult> Bind<T1, T2, TResult>(this Result<(T1, T2)> result, Func<T1, T2, Result<TResult>> func)

Parameters

result Result<(T1, T2)>

The result containing a 2-element tuple.

func Func<T1, T2, Result<TResult>>

The function to execute on the tuple elements if the result is successful.

Returns

Result<TResult>

A new Result containing the function's result, or the original failure.

Type Parameters

T1

The type of element 1 in the tuple.

T2

The type of element 2 in the tuple.

TResult

The type of the result value returned by the function.

Examples

var result = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))
    .Bind((arg1, arg2) => CreateEntity(arg1, arg2));

Bind<T1, T2, T3, TResult>(Result<(T1, T2, T3)>, Func<T1, T2, T3, Result<TResult>>)

Chains an operation that returns a Result based on a 3-element tuple value. If the current result is a failure, returns the failure without calling the function.

public static Result<TResult> Bind<T1, T2, T3, TResult>(this Result<(T1, T2, T3)> result, Func<T1, T2, T3, Result<TResult>> func)

Parameters

result Result<(T1, T2, T3)>

The result containing a 3-element tuple.

func Func<T1, T2, T3, Result<TResult>>

The function to execute on the tuple elements if the result is successful.

Returns

Result<TResult>

A new Result containing the function's result, or the original failure.

Type Parameters

T1

The type of element 1 in the tuple.

T2

The type of element 2 in the tuple.

T3

The type of element 3 in the tuple.

TResult

The type of the result value returned by the function.

Examples

var result = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))
    .Combine(Field3.TryCreate("value3"))
    .Bind((arg1, arg2, arg3) => CreateEntity(arg1, arg2, arg3));

Bind<T1, T2, T3, T4, TResult>(Result<(T1, T2, T3, T4)>, Func<T1, T2, T3, T4, Result<TResult>>)

Chains an operation that returns a Result based on a 4-element tuple value. If the current result is a failure, returns the failure without calling the function.

public static Result<TResult> Bind<T1, T2, T3, T4, TResult>(this Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, Result<TResult>> func)

Parameters

result Result<(T1, T2, T3, T4)>

The result containing a 4-element tuple.

func Func<T1, T2, T3, T4, Result<TResult>>

The function to execute on the tuple elements if the result is successful.

Returns

Result<TResult>

A new Result containing the function's result, or the original failure.

Type Parameters

T1

The type of element 1 in the tuple.

T2

The type of element 2 in the tuple.

T3

The type of element 3 in the tuple.

T4

The type of element 4 in the tuple.

TResult

The type of the result value returned by the function.

Examples

var result = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))
    .Combine(Field3.TryCreate("value3"))
    .Combine(Field4.TryCreate("value4"))
    .Bind((arg1, arg2, arg3, arg4) => CreateEntity(arg1, arg2, arg3, arg4));

Bind<T1, T2, T3, T4, T5, TResult>(Result<(T1, T2, T3, T4, T5)>, Func<T1, T2, T3, T4, T5, Result<TResult>>)

Chains an operation that returns a Result based on a 5-element tuple value. If the current result is a failure, returns the failure without calling the function.

public static Result<TResult> Bind<T1, T2, T3, T4, T5, TResult>(this Result<(T1, T2, T3, T4, T5)> result, Func<T1, T2, T3, T4, T5, Result<TResult>> func)

Parameters

result Result<(T1, T2, T3, T4, T5)>

The result containing a 5-element tuple.

func Func<T1, T2, T3, T4, T5, Result<TResult>>

The function to execute on the tuple elements if the result is successful.

Returns

Result<TResult>

A new Result containing the function's result, or the original failure.

Type Parameters

T1

The type of element 1 in the tuple.

T2

The type of element 2 in the tuple.

T3

The type of element 3 in the tuple.

T4

The type of element 4 in the tuple.

T5

The type of element 5 in the tuple.

TResult

The type of the result value returned by the function.

Examples

var result = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))
    .Combine(Field3.TryCreate("value3"))
    .Combine(Field4.TryCreate("value4"))
    .Combine(Field5.TryCreate("value5"))
    .Bind((arg1, arg2, arg3, arg4, arg5) => CreateEntity(arg1, arg2, arg3, arg4, arg5));

Bind<T1, T2, T3, T4, T5, T6, TResult>(Result<(T1, T2, T3, T4, T5, T6)>, Func<T1, T2, T3, T4, T5, T6, Result<TResult>>)

Chains an operation that returns a Result based on a 6-element tuple value. If the current result is a failure, returns the failure without calling the function.

public static Result<TResult> Bind<T1, T2, T3, T4, T5, T6, TResult>(this Result<(T1, T2, T3, T4, T5, T6)> result, Func<T1, T2, T3, T4, T5, T6, Result<TResult>> func)

Parameters

result Result<(T1, T2, T3, T4, T5, T6)>

The result containing a 6-element tuple.

func Func<T1, T2, T3, T4, T5, T6, Result<TResult>>

The function to execute on the tuple elements if the result is successful.

Returns

Result<TResult>

A new Result containing the function's result, or the original failure.

Type Parameters

T1

The type of element 1 in the tuple.

T2

The type of element 2 in the tuple.

T3

The type of element 3 in the tuple.

T4

The type of element 4 in the tuple.

T5

The type of element 5 in the tuple.

T6

The type of element 6 in the tuple.

TResult

The type of the result value returned by the function.

Examples

var result = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))
    .Combine(Field3.TryCreate("value3"))
    .Combine(Field4.TryCreate("value4"))
    .Combine(Field5.TryCreate("value5"))
    .Combine(Field6.TryCreate("value6"))
    .Bind((arg1, arg2, arg3, arg4, arg5, arg6) => CreateEntity(arg1, arg2, arg3, arg4, arg5, arg6));

Bind<T1, T2, T3, T4, T5, T6, T7, TResult>(Result<(T1, T2, T3, T4, T5, T6, T7)>, Func<T1, T2, T3, T4, T5, T6, T7, Result<TResult>>)

Chains an operation that returns a Result based on a 7-element tuple value. If the current result is a failure, returns the failure without calling the function.

public static Result<TResult> Bind<T1, T2, T3, T4, T5, T6, T7, TResult>(this Result<(T1, T2, T3, T4, T5, T6, T7)> result, Func<T1, T2, T3, T4, T5, T6, T7, Result<TResult>> func)

Parameters

result Result<(T1, T2, T3, T4, T5, T6, T7)>

The result containing a 7-element tuple.

func Func<T1, T2, T3, T4, T5, T6, T7, Result<TResult>>

The function to execute on the tuple elements if the result is successful.

Returns

Result<TResult>

A new Result containing the function's result, or the original failure.

Type Parameters

T1

The type of element 1 in the tuple.

T2

The type of element 2 in the tuple.

T3

The type of element 3 in the tuple.

T4

The type of element 4 in the tuple.

T5

The type of element 5 in the tuple.

T6

The type of element 6 in the tuple.

T7

The type of element 7 in the tuple.

TResult

The type of the result value returned by the function.

Examples

var result = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))
    .Combine(Field3.TryCreate("value3"))
    .Combine(Field4.TryCreate("value4"))
    .Combine(Field5.TryCreate("value5"))
    .Combine(Field6.TryCreate("value6"))
    .Combine(Field7.TryCreate("value7"))
    .Bind((arg1, arg2, arg3, arg4, arg5, arg6, arg7) => CreateEntity(arg1, arg2, arg3, arg4, arg5, arg6, arg7));

Bind<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(Result<(T1, T2, T3, T4, T5, T6, T7, T8)>, Func<T1, T2, T3, T4, T5, T6, T7, T8, Result<TResult>>)

Chains an operation that returns a Result based on a 8-element tuple value. If the current result is a failure, returns the failure without calling the function.

public static Result<TResult> Bind<T1, T2, T3, T4, T5, T6, T7, T8, TResult>(this Result<(T1, T2, T3, T4, T5, T6, T7, T8)> result, Func<T1, T2, T3, T4, T5, T6, T7, T8, Result<TResult>> func)

Parameters

result Result<(T1, T2, T3, T4, T5, T6, T7, T8)>

The result containing a 8-element tuple.

func Func<T1, T2, T3, T4, T5, T6, T7, T8, Result<TResult>>

The function to execute on the tuple elements if the result is successful.

Returns

Result<TResult>

A new Result containing the function's result, or the original failure.

Type Parameters

T1

The type of element 1 in the tuple.

T2

The type of element 2 in the tuple.

T3

The type of element 3 in the tuple.

T4

The type of element 4 in the tuple.

T5

The type of element 5 in the tuple.

T6

The type of element 6 in the tuple.

T7

The type of element 7 in the tuple.

T8

The type of element 8 in the tuple.

TResult

The type of the result value returned by the function.

Examples

var result = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))
    .Combine(Field3.TryCreate("value3"))
    .Combine(Field4.TryCreate("value4"))
    .Combine(Field5.TryCreate("value5"))
    .Combine(Field6.TryCreate("value6"))
    .Combine(Field7.TryCreate("value7"))
    .Combine(Field8.TryCreate("value8"))
    .Bind((arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) => CreateEntity(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));