Class OneOf<T1, T2>

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

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

public sealed class OneOf<T1, T2> : OneOfBase<T1, T2>, IOneOf<T1, T2>, IEquatable<OneOf<T1, T2>>

Type Parameters

T1

The first possible type.

T2

The second possible type.

Inheritance
OneOfBase<T1, T2>
OneOf<T1, T2>
Implements
IOneOf<T1, T2>
IEquatable<OneOf<T1, T2>>
Inherited Members
Extension Methods

Remarks

OneOf<T1, T2> provides a type-safe way to represent a value that can be one of two types. This is useful for scenarios where you need to handle different types of values or errors without using null references or exceptions.

Common use cases include: - Error handling with typed errors: OneOf<Error, Success> - Configuration values: OneOf<string, int> - API responses: OneOf<ValidationError, Data>

// Error handling
OneOf<Error, User> user = GetUser(id);
return user.Match(
    error => HandleError(error),
    user => ProcessUser(user)
);

// Configuration parsing OneOf<string, int> config = GetConfig("timeout"); int timeout = config.Match( str => int.Parse(str), num => num );

Methods

Bind<TNewT2>(Func<T2, OneOf<T1, TNewT2>>)

Binds the T2 value if present, otherwise propagates T1.

public OneOf<T1, TNewT2> Bind<TNewT2>(Func<T2, OneOf<T1, TNewT2>> binder)

Parameters

binder Func<T2, OneOf<T1, TNewT2>>

The function that takes T2 and returns a OneOf.

Returns

OneOf<T1, TNewT2>

The result of the binder function or the original T1.

Type Parameters

TNewT2

The new T2 type.

Examples

OneOf<Error, int> userId = GetUserId();
OneOf<Error, User> user = userId.Bind(id => GetUser(id));

Exceptions

ArgumentNullException

Thrown when binder is null.

Equals(OneOf<T1, T2>?)

Determines equality between two OneOf instances.

public bool Equals(OneOf<T1, T2>? other)

Parameters

other OneOf<T1, T2>

The other OneOf to compare with.

Returns

bool

true if the OneOf instances are equal; otherwise, false.

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

Filter(Func<T2, bool>, T1)

Filters the T2 value if it satisfies the predicate, otherwise converts to T1.

public OneOf<T1, T2> Filter(Func<T2, bool> predicate, T1 fallbackT1)

Parameters

predicate Func<T2, bool>

The condition to test the T2 value against.

fallbackT1 T1

The T1 value to use when the predicate fails.

Returns

OneOf<T1, T2>

The original OneOf if the predicate is true, otherwise the fallback T1.

Examples

OneOf<Error, User> user = GetUser(id);
OneOf<Error, User> activeUser = user.Filter(u => u.IsActive, new UserInactiveError());

Exceptions

ArgumentNullException

Thrown when predicate is null.

FromT1(T1)

Creates a OneOf containing a T1 value.

public static OneOf<T1, T2> FromT1(T1 value)

Parameters

value T1

The T1 value to wrap.

Returns

OneOf<T1, T2>

A OneOf containing the specified T1 value.

Examples

OneOf<Error, User> result = OneOf<Error, User>.FromT1(new NotFoundError());

FromT2(T2)

Creates a OneOf containing a T2 value.

public static OneOf<T1, T2> FromT2(T2 value)

Parameters

value T2

The T2 value to wrap.

Returns

OneOf<T1, T2>

A OneOf containing the specified T2 value.

Examples

OneOf<Error, User> result = OneOf<Error, User>.FromT2(new User("Alice"));

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

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

Maps the T2 value if present, otherwise propagates T1.

public OneOf<T1, TNewT2> Map<TNewT2>(Func<T2, TNewT2> mapper)

Parameters

mapper Func<T2, TNewT2>

The function to apply to the T2 value.

Returns

OneOf<T1, TNewT2>

A new OneOf with the mapped T2 value or the original T1.

Type Parameters

TNewT2

The new T2 type.

Examples

OneOf<Error, User> user = GetUser(id);
OneOf<Error, string> userName = user.Map(u => u.Name);

Exceptions

ArgumentNullException

Thrown when mapper is null.

Operators

operator ==(OneOf<T1, T2>?, OneOf<T1, T2>?)

Equality operator for OneOf instances.

public static bool operator ==(OneOf<T1, T2>? left, OneOf<T1, T2>? right)

Parameters

left OneOf<T1, T2>
right OneOf<T1, T2>

Returns

bool

implicit operator OneOf<T1, T2>(T1)

Implicit conversion from T1 to OneOf<T1, T2>.

public static implicit operator OneOf<T1, T2>(T1 value)

Parameters

value T1

Returns

OneOf<T1, T2>

Examples

OneOf<Error, User> result = new NotFoundError(); // Implicit conversion

implicit operator OneOf<T1, T2>(T2)

Implicit conversion from T2 to OneOf<T1, T2>.

public static implicit operator OneOf<T1, T2>(T2 value)

Parameters

value T2

Returns

OneOf<T1, T2>

Examples

OneOf<Error, User> result = new User("Alice"); // Implicit conversion

operator !=(OneOf<T1, T2>?, OneOf<T1, T2>?)

Inequality operator for OneOf instances.

public static bool operator !=(OneOf<T1, T2>? left, OneOf<T1, T2>? right)

Parameters

left OneOf<T1, T2>
right OneOf<T1, T2>

Returns

bool