Struct OneOf<T1, T2, T3, T4, T5, T6>

Namespace
REslava.Result.AdvancedPatterns
Assembly
REslava.Result.dll

Represents a value that can be one of six possible types. A type-safe discriminated union for functional programming patterns.

public readonly struct OneOf<T1, T2, T3, T4, T5, T6> : IEquatable<OneOf<T1, T2, T3, T4, T5, T6>>

Type Parameters

T1

The first possible type.

T2

The second possible type.

T3

The third possible type.

T4

The fourth possible type.

T5

The fifth possible type.

T6

The sixth possible type.

Implements
IEquatable<OneOf<T1, T2, T3, T4, T5, T6>>
Inherited Members
Extension Methods

Remarks

OneOf<T1, T2, T3, T4, T5, T6> provides a type-safe way to represent a value that can be one of six types.

OneOf<Success, ValidationError, NotFoundError, ConflictError, UnauthorizedError, ServerError> response = await CallApi();
return response.Match(
    case1: data => ProcessData(data),
    case2: err => HandleValidation(err),
    case3: err => HandleNotFound(err),
    case4: err => HandleConflict(err),
    case5: err => HandleUnauthorized(err),
    case6: err => HandleServerError(err)
);

Properties

AsT1

Gets the value as T1 if it contains T1, otherwise throws InvalidOperationException.

public T1 AsT1 { get; }

Property Value

T1

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T1.

AsT2

Gets the value as T2 if it contains T2, otherwise throws InvalidOperationException.

public T2 AsT2 { get; }

Property Value

T2

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T2.

AsT3

Gets the value as T3 if it contains T3, otherwise throws InvalidOperationException.

public T3 AsT3 { get; }

Property Value

T3

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T3.

AsT4

Gets the value as T4 if it contains T4, otherwise throws InvalidOperationException.

public T4 AsT4 { get; }

Property Value

T4

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T4.

AsT5

Gets the value as T5 if it contains T5, otherwise throws InvalidOperationException.

public T5 AsT5 { get; }

Property Value

T5

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T5.

AsT6

Gets the value as T6 if it contains T6, otherwise throws InvalidOperationException.

public T6 AsT6 { get; }

Property Value

T6

Exceptions

InvalidOperationException

Thrown when the OneOf does not contain T6.

IsT1

Gets whether the OneOf contains a value of type T1.

public bool IsT1 { get; }

Property Value

bool

IsT2

Gets whether the OneOf contains a value of type T2.

public bool IsT2 { get; }

Property Value

bool

IsT3

Gets whether the OneOf contains a value of type T3.

public bool IsT3 { get; }

Property Value

bool

IsT4

Gets whether the OneOf contains a value of type T4.

public bool IsT4 { get; }

Property Value

bool

IsT5

Gets whether the OneOf contains a value of type T5.

public bool IsT5 { get; }

Property Value

bool

IsT6

Gets whether the OneOf contains a value of type T6.

public bool IsT6 { get; }

Property Value

bool

Methods

BindT2<TNewT2>(Func<T2, OneOf<T1, TNewT2, T3, T4, T5, T6>>)

Binds the T2 value if present, otherwise propagates other types.

public OneOf<T1, TNewT2, T3, T4, T5, T6> BindT2<TNewT2>(Func<T2, OneOf<T1, TNewT2, T3, T4, T5, T6>> binder)

Parameters

binder Func<T2, OneOf<T1, TNewT2, T3, T4, T5, T6>>

Returns

OneOf<T1, TNewT2, T3, T4, T5, T6>

The result of the binder function or the original T1/T3/T4/T5/T6.

Type Parameters

TNewT2

Exceptions

ArgumentNullException

Thrown when binder is null.

BindT3<TNewT3>(Func<T3, OneOf<T1, T2, TNewT3, T4, T5, T6>>)

Binds the T3 value if present, otherwise propagates other types.

public OneOf<T1, T2, TNewT3, T4, T5, T6> BindT3<TNewT3>(Func<T3, OneOf<T1, T2, TNewT3, T4, T5, T6>> binder)

Parameters

binder Func<T3, OneOf<T1, T2, TNewT3, T4, T5, T6>>

Returns

OneOf<T1, T2, TNewT3, T4, T5, T6>

The result of the binder function or the original T1/T2/T4/T5/T6.

Type Parameters

TNewT3

Exceptions

ArgumentNullException

Thrown when binder is null.

BindT4<TNewT4>(Func<T4, OneOf<T1, T2, T3, TNewT4, T5, T6>>)

Binds the T4 value if present, otherwise propagates other types.

public OneOf<T1, T2, T3, TNewT4, T5, T6> BindT4<TNewT4>(Func<T4, OneOf<T1, T2, T3, TNewT4, T5, T6>> binder)

Parameters

binder Func<T4, OneOf<T1, T2, T3, TNewT4, T5, T6>>

Returns

OneOf<T1, T2, T3, TNewT4, T5, T6>

The result of the binder function or the original T1/T2/T3/T5/T6.

Type Parameters

TNewT4

Exceptions

ArgumentNullException

Thrown when binder is null.

BindT5<TNewT5>(Func<T5, OneOf<T1, T2, T3, T4, TNewT5, T6>>)

Binds the T5 value if present, otherwise propagates other types.

public OneOf<T1, T2, T3, T4, TNewT5, T6> BindT5<TNewT5>(Func<T5, OneOf<T1, T2, T3, T4, TNewT5, T6>> binder)

Parameters

binder Func<T5, OneOf<T1, T2, T3, T4, TNewT5, T6>>

Returns

OneOf<T1, T2, T3, T4, TNewT5, T6>

The result of the binder function or the original T1/T2/T3/T4/T6.

Type Parameters

TNewT5

Exceptions

ArgumentNullException

Thrown when binder is null.

Equals(OneOf<T1, T2, T3, T4, T5, T6>)

Indicates whether the current OneOf is equal to another OneOf of the same type.

public bool Equals(OneOf<T1, T2, T3, T4, T5, T6> other)

Parameters

other OneOf<T1, T2, T3, T4, T5, T6>

Returns

bool

Equals(object?)

Indicates whether the current OneOf is equal to another object.

public override bool Equals(object? obj)

Parameters

obj object

Returns

bool

FromT1(T1)

Creates a OneOf containing a T1 value.

public static OneOf<T1, T2, T3, T4, T5, T6> FromT1(T1 value)

Parameters

value T1

Returns

OneOf<T1, T2, T3, T4, T5, T6>

FromT2(T2)

Creates a OneOf containing a T2 value.

public static OneOf<T1, T2, T3, T4, T5, T6> FromT2(T2 value)

Parameters

value T2

Returns

OneOf<T1, T2, T3, T4, T5, T6>

FromT3(T3)

Creates a OneOf containing a T3 value.

public static OneOf<T1, T2, T3, T4, T5, T6> FromT3(T3 value)

Parameters

value T3

Returns

OneOf<T1, T2, T3, T4, T5, T6>

FromT4(T4)

Creates a OneOf containing a T4 value.

public static OneOf<T1, T2, T3, T4, T5, T6> FromT4(T4 value)

Parameters

value T4

Returns

OneOf<T1, T2, T3, T4, T5, T6>

FromT5(T5)

Creates a OneOf containing a T5 value.

public static OneOf<T1, T2, T3, T4, T5, T6> FromT5(T5 value)

Parameters

value T5

Returns

OneOf<T1, T2, T3, T4, T5, T6>

FromT6(T6)

Creates a OneOf containing a T6 value.

public static OneOf<T1, T2, T3, T4, T5, T6> FromT6(T6 value)

Parameters

value T6

Returns

OneOf<T1, T2, T3, T4, T5, T6>

GetHashCode()

Returns the hash code for the OneOf.

public override int GetHashCode()

Returns

int

MapT2<TNewT2>(Func<T2, TNewT2>)

Maps the T2 value if present, otherwise propagates other types.

public OneOf<T1, TNewT2, T3, T4, T5, T6> MapT2<TNewT2>(Func<T2, TNewT2> mapper)

Parameters

mapper Func<T2, TNewT2>

Returns

OneOf<T1, TNewT2, T3, T4, T5, T6>

A new OneOf with the mapped T2 value or the original T1/T3/T4/T5/T6.

Type Parameters

TNewT2

Exceptions

ArgumentNullException

Thrown when mapper is null.

MapT3<TNewT3>(Func<T3, TNewT3>)

Maps the T3 value if present, otherwise propagates other types.

public OneOf<T1, T2, TNewT3, T4, T5, T6> MapT3<TNewT3>(Func<T3, TNewT3> mapper)

Parameters

mapper Func<T3, TNewT3>

Returns

OneOf<T1, T2, TNewT3, T4, T5, T6>

A new OneOf with the mapped T3 value or the original T1/T2/T4/T5/T6.

Type Parameters

TNewT3

Exceptions

ArgumentNullException

Thrown when mapper is null.

MapT4<TNewT4>(Func<T4, TNewT4>)

Maps the T4 value if present, otherwise propagates other types.

public OneOf<T1, T2, T3, TNewT4, T5, T6> MapT4<TNewT4>(Func<T4, TNewT4> mapper)

Parameters

mapper Func<T4, TNewT4>

Returns

OneOf<T1, T2, T3, TNewT4, T5, T6>

A new OneOf with the mapped T4 value or the original T1/T2/T3/T5/T6.

Type Parameters

TNewT4

Exceptions

ArgumentNullException

Thrown when mapper is null.

MapT5<TNewT5>(Func<T5, TNewT5>)

Maps the T5 value if present, otherwise propagates other types.

public OneOf<T1, T2, T3, T4, TNewT5, T6> MapT5<TNewT5>(Func<T5, TNewT5> mapper)

Parameters

mapper Func<T5, TNewT5>

Returns

OneOf<T1, T2, T3, T4, TNewT5, T6>

A new OneOf with the mapped T5 value or the original T1/T2/T3/T4/T6.

Type Parameters

TNewT5

Exceptions

ArgumentNullException

Thrown when mapper is null.

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

Pattern matching — executes the appropriate function based on the contained type.

public TResult Match<TResult>(Func<T1, TResult> case1, Func<T2, TResult> case2, Func<T3, TResult> case3, Func<T4, TResult> case4, Func<T5, TResult> case5, Func<T6, TResult> case6)

Parameters

case1 Func<T1, TResult>
case2 Func<T2, TResult>
case3 Func<T3, TResult>
case4 Func<T4, TResult>
case5 Func<T5, TResult>
case6 Func<T6, TResult>

Returns

TResult

Type Parameters

TResult

The type of the result.

Exceptions

ArgumentNullException

Thrown when any case function is null.

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

Executes an action based on the contained type.

public void Switch(Action<T1> case1, Action<T2> case2, Action<T3> case3, Action<T4> case4, Action<T5> case5, Action<T6> case6)

Parameters

case1 Action<T1>
case2 Action<T2>
case3 Action<T3>
case4 Action<T4>
case5 Action<T5>
case6 Action<T6>

Exceptions

ArgumentNullException

Thrown when any case action is null.

ToString()

Returns a string representation of the OneOf for debugging.

public override string ToString()

Returns

string

Operators

operator ==(OneOf<T1, T2, T3, T4, T5, T6>, OneOf<T1, T2, T3, T4, T5, T6>)

Determines whether two OneOf instances are equal.

public static bool operator ==(OneOf<T1, T2, T3, T4, T5, T6> left, OneOf<T1, T2, T3, T4, T5, T6> right)

Parameters

left OneOf<T1, T2, T3, T4, T5, T6>
right OneOf<T1, T2, T3, T4, T5, T6>

Returns

bool

implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T1)

Implicit conversion from T1 to OneOf<T1, T2, T3, T4, T5, T6>.

public static implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T1 value)

Parameters

value T1

Returns

OneOf<T1, T2, T3, T4, T5, T6>

implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T2)

Implicit conversion from T2 to OneOf<T1, T2, T3, T4, T5, T6>.

public static implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T2 value)

Parameters

value T2

Returns

OneOf<T1, T2, T3, T4, T5, T6>

implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T3)

Implicit conversion from T3 to OneOf<T1, T2, T3, T4, T5, T6>.

public static implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T3 value)

Parameters

value T3

Returns

OneOf<T1, T2, T3, T4, T5, T6>

implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T4)

Implicit conversion from T4 to OneOf<T1, T2, T3, T4, T5, T6>.

public static implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T4 value)

Parameters

value T4

Returns

OneOf<T1, T2, T3, T4, T5, T6>

implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T5)

Implicit conversion from T5 to OneOf<T1, T2, T3, T4, T5, T6>.

public static implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T5 value)

Parameters

value T5

Returns

OneOf<T1, T2, T3, T4, T5, T6>

implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T6)

Implicit conversion from T6 to OneOf<T1, T2, T3, T4, T5, T6>.

public static implicit operator OneOf<T1, T2, T3, T4, T5, T6>(T6 value)

Parameters

value T6

Returns

OneOf<T1, T2, T3, T4, T5, T6>

operator !=(OneOf<T1, T2, T3, T4, T5, T6>, OneOf<T1, T2, T3, T4, T5, T6>)

Determines whether two OneOf instances are not equal.

public static bool operator !=(OneOf<T1, T2, T3, T4, T5, T6> left, OneOf<T1, T2, T3, T4, T5, T6> right)

Parameters

left OneOf<T1, T2, T3, T4, T5, T6>
right OneOf<T1, T2, T3, T4, T5, T6>

Returns

bool