Table of Contents

Class MatchTupleExtensions

Namespace
Trellis
Assembly
Trellis.Core.dll

Provides pattern matching extension methods for Result types containing tuples (2-5 elements) with automatic destructuring. Match enables handling both success and failure cases inline with clean, readable code.

public static class MatchTupleExtensions
Inheritance
MatchTupleExtensions
Inherited Members

Examples

var response = EmailAddress.TryCreate(email)
    .Combine(FirstName.TryCreate(firstName))
    .Match(
        onSuccess: (email, firstName) => Ok(new UserDto { Email = email, Name = firstName }),
        onFailure: error => BadRequest(error.Detail)
    );

Remarks

These overloads automatically destructure tuple values, eliminating the need to manually extract tuple elements. This makes pattern matching on combined results more ergonomic and type-safe.

Key features:

  • Automatic tuple destructuring - no manual element access needed
  • Type-safe handling of both success and failure paths
  • Compile-time verification of tuple element types
  • Clean, readable syntax for complex result handling

Use Match when you need to:

  • Convert Result to another type (e.g., HTTP response, DTO)
  • Handle both success and failure paths explicitly
  • Work with tuple values from Combine operations
  • Transform results at API boundaries

Methods

Match<T1, T2, TOut>(Result<(T1, T2)>, Func<T1, T2, TOut>, Func<Error, TOut>)

Matches on a Result containing a 2-element tuple, automatically destructuring the tuple values.

public static TOut Match<T1, T2, TOut>(this Result<(T1, T2)> result, Func<T1, T2, TOut> onSuccess, Func<Error, TOut> onFailure)

Parameters

result Result<(T1, T2)>

The result to match on.

onSuccess Func<T1, T2, TOut>

Function to execute on success, receiving 2 destructured tuple values as separate parameters.

onFailure Func<Error, TOut>

Function to execute on failure, receiving the error.

Returns

TOut

The output from either the success or failure function.

Type Parameters

T1

The type of the first tuple element.

T2

The type of the second tuple element.

TOut

The type of the output from either function.

Examples

var greeting = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))    ///     .Match(
        onSuccess: (v1, v2) => $"Hello, {v1 + ", " + v2}!",
        onFailure: error => $"Error: {error.Detail}"
    );

Remarks

The tuple is automatically destructured, so you receive individual values rather than accessing tuple members. This provides cleaner, more readable code compared to manual tuple access.

Match<T1, T2, T3, TOut>(Result<(T1, T2, T3)>, Func<T1, T2, T3, TOut>, Func<Error, TOut>)

Matches on a Result containing a 3-element tuple, automatically destructuring the tuple values.

public static TOut Match<T1, T2, T3, TOut>(this Result<(T1, T2, T3)> result, Func<T1, T2, T3, TOut> onSuccess, Func<Error, TOut> onFailure)

Parameters

result Result<(T1, T2, T3)>

The result to match on.

onSuccess Func<T1, T2, T3, TOut>

Function to execute on success, receiving 3 destructured tuple values as separate parameters.

onFailure Func<Error, TOut>

Function to execute on failure, receiving the error.

Returns

TOut

The output from either the success or failure function.

Type Parameters

T1

The type of the first tuple element.

T2

The type of the second tuple element.

T3

The type of the third tuple element.

TOut

The type of the output from either function.

Examples

var greeting = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))    ///     .Combine(Field3.TryCreate("value3"))    ///     .Match(
        onSuccess: (v1, v2, v3) => $"Hello, {v1 + ", " + v2 + ", " + v3}!",
        onFailure: error => $"Error: {error.Detail}"
    );

Remarks

The tuple is automatically destructured, so you receive individual values rather than accessing tuple members. This provides cleaner, more readable code compared to manual tuple access.

Match<T1, T2, T3, T4, TOut>(Result<(T1, T2, T3, T4)>, Func<T1, T2, T3, T4, TOut>, Func<Error, TOut>)

Matches on a Result containing a 4-element tuple, automatically destructuring the tuple values.

public static TOut Match<T1, T2, T3, T4, TOut>(this Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, TOut> onSuccess, Func<Error, TOut> onFailure)

Parameters

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

The result to match on.

onSuccess Func<T1, T2, T3, T4, TOut>

Function to execute on success, receiving 4 destructured tuple values as separate parameters.

onFailure Func<Error, TOut>

Function to execute on failure, receiving the error.

Returns

TOut

The output from either the success or failure function.

Type Parameters

T1

The type of the first tuple element.

T2

The type of the second tuple element.

T3

The type of the third tuple element.

T4

The type of the fourth tuple element.

TOut

The type of the output from either function.

Examples

var greeting = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))    ///     .Combine(Field3.TryCreate("value3"))    ///     .Combine(Field4.TryCreate("value4"))    ///     .Match(
        onSuccess: (v1, v2, v3, v4) => $"Hello, {v1 + ", " + v2 + ", " + v3 + ", " + v4}!",
        onFailure: error => $"Error: {error.Detail}"
    );

Remarks

The tuple is automatically destructured, so you receive individual values rather than accessing tuple members. This provides cleaner, more readable code compared to manual tuple access.

Match<T1, T2, T3, T4, T5, TOut>(Result<(T1, T2, T3, T4, T5)>, Func<T1, T2, T3, T4, T5, TOut>, Func<Error, TOut>)

Matches on a Result containing a 5-element tuple, automatically destructuring the tuple values.

public static TOut Match<T1, T2, T3, T4, T5, TOut>(this Result<(T1, T2, T3, T4, T5)> result, Func<T1, T2, T3, T4, T5, TOut> onSuccess, Func<Error, TOut> onFailure)

Parameters

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

The result to match on.

onSuccess Func<T1, T2, T3, T4, T5, TOut>

Function to execute on success, receiving 5 destructured tuple values as separate parameters.

onFailure Func<Error, TOut>

Function to execute on failure, receiving the error.

Returns

TOut

The output from either the success or failure function.

Type Parameters

T1

The type of the first tuple element.

T2

The type of the second tuple element.

T3

The type of the third tuple element.

T4

The type of the fourth tuple element.

T5

The type of the fifth tuple element.

TOut

The type of the output from either function.

Examples

var greeting = FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))    ///     .Combine(Field3.TryCreate("value3"))    ///     .Combine(Field4.TryCreate("value4"))    ///     .Combine(Field5.TryCreate("value5"))    ///     .Match(
        onSuccess: (v1, v2, v3, v4, v5) => $"Hello, {v1 + ", " + v2 + ", " + v3 + ", " + v4 + ", " + v5}!",
        onFailure: error => $"Error: {error.Detail}"
    );

Remarks

The tuple is automatically destructured, so you receive individual values rather than accessing tuple members. This provides cleaner, more readable code compared to manual tuple access.

Switch<T1, T2>(Result<(T1, T2)>, Action<T1, T2>, Action<Error>)

Switches on a Result containing a 2-element tuple, automatically destructuring the tuple values for side effects.

public static void Switch<T1, T2>(this Result<(T1, T2)> result, Action<T1, T2> onSuccess, Action<Error> onFailure)

Parameters

result Result<(T1, T2)>

The result to switch on.

onSuccess Action<T1, T2>

Action to execute on success, receiving 2 destructured tuple values as separate parameters.

onFailure Action<Error>

Action to execute on failure, receiving the error.

Type Parameters

T1

The type of the first tuple element.

T2

The type of the second tuple element.

Examples

FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))    ///     .Switch(
        onSuccess: (v1, v2) => Console.WriteLine($"Success: {v1 + ", " + v2}"),
        onFailure: error => Console.WriteLine($"Error: {error.Detail}")
    );

Remarks

Unlike Match which returns a value, Switch executes actions for side effects only. Use Switch when you need to perform operations but don't need to return a result.

Switch<T1, T2, T3>(Result<(T1, T2, T3)>, Action<T1, T2, T3>, Action<Error>)

Switches on a Result containing a 3-element tuple, automatically destructuring the tuple values for side effects.

public static void Switch<T1, T2, T3>(this Result<(T1, T2, T3)> result, Action<T1, T2, T3> onSuccess, Action<Error> onFailure)

Parameters

result Result<(T1, T2, T3)>

The result to switch on.

onSuccess Action<T1, T2, T3>

Action to execute on success, receiving 3 destructured tuple values as separate parameters.

onFailure Action<Error>

Action to execute on failure, receiving the error.

Type Parameters

T1

The type of the first tuple element.

T2

The type of the second tuple element.

T3

The type of the third tuple element.

Examples

FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))    ///     .Combine(Field3.TryCreate("value3"))    ///     .Switch(
        onSuccess: (v1, v2, v3) => Console.WriteLine($"Success: {v1 + ", " + v2 + ", " + v3}"),
        onFailure: error => Console.WriteLine($"Error: {error.Detail}")
    );

Remarks

Unlike Match which returns a value, Switch executes actions for side effects only. Use Switch when you need to perform operations but don't need to return a result.

Switch<T1, T2, T3, T4>(Result<(T1, T2, T3, T4)>, Action<T1, T2, T3, T4>, Action<Error>)

Switches on a Result containing a 4-element tuple, automatically destructuring the tuple values for side effects.

public static void Switch<T1, T2, T3, T4>(this Result<(T1, T2, T3, T4)> result, Action<T1, T2, T3, T4> onSuccess, Action<Error> onFailure)

Parameters

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

The result to switch on.

onSuccess Action<T1, T2, T3, T4>

Action to execute on success, receiving 4 destructured tuple values as separate parameters.

onFailure Action<Error>

Action to execute on failure, receiving the error.

Type Parameters

T1

The type of the first tuple element.

T2

The type of the second tuple element.

T3

The type of the third tuple element.

T4

The type of the fourth tuple element.

Examples

FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))    ///     .Combine(Field3.TryCreate("value3"))    ///     .Combine(Field4.TryCreate("value4"))    ///     .Switch(
        onSuccess: (v1, v2, v3, v4) => Console.WriteLine($"Success: {v1 + ", " + v2 + ", " + v3 + ", " + v4}"),
        onFailure: error => Console.WriteLine($"Error: {error.Detail}")
    );

Remarks

Unlike Match which returns a value, Switch executes actions for side effects only. Use Switch when you need to perform operations but don't need to return a result.

Switch<T1, T2, T3, T4, T5>(Result<(T1, T2, T3, T4, T5)>, Action<T1, T2, T3, T4, T5>, Action<Error>)

Switches on a Result containing a 5-element tuple, automatically destructuring the tuple values for side effects.

public static void Switch<T1, T2, T3, T4, T5>(this Result<(T1, T2, T3, T4, T5)> result, Action<T1, T2, T3, T4, T5> onSuccess, Action<Error> onFailure)

Parameters

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

The result to switch on.

onSuccess Action<T1, T2, T3, T4, T5>

Action to execute on success, receiving 5 destructured tuple values as separate parameters.

onFailure Action<Error>

Action to execute on failure, receiving the error.

Type Parameters

T1

The type of the first tuple element.

T2

The type of the second tuple element.

T3

The type of the third tuple element.

T4

The type of the fourth tuple element.

T5

The type of the fifth tuple element.

Examples

FirstName.TryCreate("John")
    .Combine(LastName.TryCreate("Doe"))    ///     .Combine(Field3.TryCreate("value3"))    ///     .Combine(Field4.TryCreate("value4"))    ///     .Combine(Field5.TryCreate("value5"))    ///     .Switch(
        onSuccess: (v1, v2, v3, v4, v5) => Console.WriteLine($"Success: {v1 + ", " + v2 + ", " + v3 + ", " + v4 + ", " + v5}"),
        onFailure: error => Console.WriteLine($"Error: {error.Detail}")
    );

Remarks

Unlike Match which returns a value, Switch executes actions for side effects only. Use Switch when you need to perform operations but don't need to return a result.