Class OneOfExtensions
- Namespace
- REslava.Result.AdvancedPatterns
- Assembly
- REslava.Result.dll
Extension methods for OneOf types, providing conversion between 2-way through 6-way OneOf instances.
public static class OneOfExtensions
- Inheritance
-
OneOfExtensions
- Inherited Members
Remarks
These extension methods provide explicit, type-safe conversions across the full OneOf arity chain: OneOf<T1,T2> ↔ OneOf<T1,T2,T3> ↔ OneOf<T1,T2,T3,T4> ↔ OneOf<T1,T2,T3,T4,T5> ↔ OneOf<T1,T2,T3,T4,T5,T6>. All conversions are explicit to ensure developers make conscious decisions about how to handle the additional type.
The design philosophy is explicit over implicit - conversions require developers to specify how to handle the removed type, preventing accidental data loss or unexpected behavior.
Methods
ToFiveWayWithFallback<T1, T2, T3, T4, T5, T6>(OneOf<T1, T2, T3, T4, T5, T6>, T1?, T2?, T3?, T4?, T5?)
Converts a 6-way OneOf to a 5-way OneOf by replacing the T6 case with a fallback value.
public static OneOf<T1, T2, T3, T4, T5> ToFiveWayWithFallback<T1, T2, T3, T4, T5, T6>(this OneOf<T1, T2, T3, T4, T5, T6> oneOf, T1? fallbackT1 = null, T2? fallbackT2 = null, T3? fallbackT3 = null, T4? fallbackT4 = null, T5? fallbackT5 = null) where T1 : class where T2 : class where T3 : class where T4 : class where T5 : class
Parameters
oneOfOneOf<T1, T2, T3, T4, T5, T6>fallbackT1T1fallbackT2T2fallbackT3T3fallbackT4T4fallbackT5T5
Returns
- OneOf<T1, T2, T3, T4, T5>
Type Parameters
T1T2T3T4T5T6
ToFiveWay<T1, T2, T3, T4, T5>(OneOf<T1, T2, T3, T4>, T5)
Converts a 4-way OneOf to a 5-way OneOf by providing a default value for the fifth type.
public static OneOf<T1, T2, T3, T4, T5> ToFiveWay<T1, T2, T3, T4, T5>(this OneOf<T1, T2, T3, T4> oneOf, T5 defaultValue)
Parameters
oneOfOneOf<T1, T2, T3, T4>The 4-way OneOf to convert.
defaultValueT5Anchors the T5 type parameter; not used at runtime.
Returns
- OneOf<T1, T2, T3, T4, T5>
Type Parameters
T1T2T3T4T5
ToFiveWay<T1, T2, T3, T4, T5, T6>(OneOf<T1, T2, T3, T4, T5, T6>)
Converts a 6-way OneOf to a 5-way OneOf, returning null when the value is T6.
public static OneOf<T1, T2, T3, T4, T5>? ToFiveWay<T1, T2, T3, T4, T5, T6>(this OneOf<T1, T2, T3, T4, T5, T6> oneOf)
Parameters
oneOfOneOf<T1, T2, T3, T4, T5, T6>
Returns
- OneOf<T1, T2, T3, T4, T5>?
Type Parameters
T1T2T3T4T5T6
ToFiveWay<T1, T2, T3, T4, T5, T6>(OneOf<T1, T2, T3, T4, T5, T6>, Func<T6, T1>?, Func<T6, T2>?, Func<T6, T3>?, Func<T6, T4>?, Func<T6, T5>?)
Converts a 6-way OneOf to a 5-way OneOf by mapping the T6 case to one of the first five types.
public static OneOf<T1, T2, T3, T4, T5> ToFiveWay<T1, T2, T3, T4, T5, T6>(this OneOf<T1, T2, T3, T4, T5, T6> oneOf, Func<T6, T1>? t6ToT1 = null, Func<T6, T2>? t6ToT2 = null, Func<T6, T3>? t6ToT3 = null, Func<T6, T4>? t6ToT4 = null, Func<T6, T5>? t6ToT5 = null)
Parameters
oneOfOneOf<T1, T2, T3, T4, T5, T6>t6ToT1Func<T6, T1>t6ToT2Func<T6, T2>t6ToT3Func<T6, T3>t6ToT4Func<T6, T4>t6ToT5Func<T6, T5>
Returns
- OneOf<T1, T2, T3, T4, T5>
Type Parameters
T1T2T3T4T5T6
ToFourWayWithFallback<T1, T2, T3, T4, T5>(OneOf<T1, T2, T3, T4, T5>, T1?, T2?, T3?, T4?)
Converts a 5-way OneOf to a 4-way OneOf by replacing the T5 case with a fallback value.
public static OneOf<T1, T2, T3, T4> ToFourWayWithFallback<T1, T2, T3, T4, T5>(this OneOf<T1, T2, T3, T4, T5> oneOf, T1? fallbackT1 = null, T2? fallbackT2 = null, T3? fallbackT3 = null, T4? fallbackT4 = null) where T1 : class where T2 : class where T3 : class where T4 : class
Parameters
oneOfOneOf<T1, T2, T3, T4, T5>fallbackT1T1fallbackT2T2fallbackT3T3fallbackT4T4
Returns
- OneOf<T1, T2, T3, T4>
Type Parameters
T1T2T3T4T5
ToFourWay<T1, T2, T3, T4>(OneOf<T1, T2, T3>, T4)
Converts a 3-way OneOf to a 4-way OneOf by providing a default value for the fourth type.
public static OneOf<T1, T2, T3, T4> ToFourWay<T1, T2, T3, T4>(this OneOf<T1, T2, T3> oneOf, T4 defaultValue)
Parameters
oneOfOneOf<T1, T2, T3>The 3-way OneOf to convert.
defaultValueT4Anchors the T4 type parameter; not used at runtime.
Returns
- OneOf<T1, T2, T3, T4>
Type Parameters
T1T2T3T4
ToFourWay<T1, T2, T3, T4, T5>(OneOf<T1, T2, T3, T4, T5>)
Converts a 5-way OneOf to a 4-way OneOf, returning null when the value is T5.
public static OneOf<T1, T2, T3, T4>? ToFourWay<T1, T2, T3, T4, T5>(this OneOf<T1, T2, T3, T4, T5> oneOf)
Parameters
oneOfOneOf<T1, T2, T3, T4, T5>
Returns
- OneOf<T1, T2, T3, T4>?
Type Parameters
T1T2T3T4T5
ToFourWay<T1, T2, T3, T4, T5>(OneOf<T1, T2, T3, T4, T5>, Func<T5, T1>?, Func<T5, T2>?, Func<T5, T3>?, Func<T5, T4>?)
Converts a 5-way OneOf to a 4-way OneOf by mapping the T5 case to one of the first four types.
public static OneOf<T1, T2, T3, T4> ToFourWay<T1, T2, T3, T4, T5>(this OneOf<T1, T2, T3, T4, T5> oneOf, Func<T5, T1>? t5ToT1 = null, Func<T5, T2>? t5ToT2 = null, Func<T5, T3>? t5ToT3 = null, Func<T5, T4>? t5ToT4 = null)
Parameters
oneOfOneOf<T1, T2, T3, T4, T5>t5ToT1Func<T5, T1>t5ToT2Func<T5, T2>t5ToT3Func<T5, T3>t5ToT4Func<T5, T4>
Returns
- OneOf<T1, T2, T3, T4>
Type Parameters
T1T2T3T4T5
ToSixWay<T1, T2, T3, T4, T5, T6>(OneOf<T1, T2, T3, T4, T5>, T6)
Converts a 5-way OneOf to a 6-way OneOf by providing a default value for the sixth type.
public static OneOf<T1, T2, T3, T4, T5, T6> ToSixWay<T1, T2, T3, T4, T5, T6>(this OneOf<T1, T2, T3, T4, T5> oneOf, T6 defaultValue)
Parameters
oneOfOneOf<T1, T2, T3, T4, T5>The 5-way OneOf to convert.
defaultValueT6Anchors the T6 type parameter; not used at runtime.
Returns
- OneOf<T1, T2, T3, T4, T5, T6>
Type Parameters
T1T2T3T4T5T6
ToThreeWayWithFallback<T1, T2, T3, T4>(OneOf<T1, T2, T3, T4>, T1?, T2?, T3?)
Converts a 4-way OneOf to a 3-way OneOf by replacing the T4 case with a fallback value.
public static OneOf<T1, T2, T3> ToThreeWayWithFallback<T1, T2, T3, T4>(this OneOf<T1, T2, T3, T4> oneOf, T1? fallbackT1 = null, T2? fallbackT2 = null, T3? fallbackT3 = null) where T1 : class where T2 : class where T3 : class
Parameters
oneOfOneOf<T1, T2, T3, T4>fallbackT1T1fallbackT2T2fallbackT3T3
Returns
- OneOf<T1, T2, T3>
Type Parameters
T1T2T3T4
ToThreeWay<T1, T2, T3>(OneOf<T1, T2>, T3)
Converts a 2-way OneOf to a 3-way OneOf by providing a default value for the third type.
public static OneOf<T1, T2, T3> ToThreeWay<T1, T2, T3>(this OneOf<T1, T2> oneOf, T3 defaultValue)
Parameters
oneOfOneOf<T1, T2>The 2-way OneOf to convert.
defaultValueT3The default value to use if the 3-way OneOf needs to represent T3.
Returns
- OneOf<T1, T2, T3>
A 3-way OneOf containing the original value or the default for T3.
Type Parameters
T1The first type.
T2The second type.
T3The third type.
Examples
OneOf<Error, Success> twoWay = new Success("Data processed");
OneOf<Error, Success, Warning> threeWay = twoWay.ToThreeWay<Warning>(new Warning("No warnings"));
Remarks
This conversion is safe and preserves the original value. The default value is only used when the 3-way OneOf needs to represent a T3 value in other operations.
ToThreeWay<T1, T2, T3, T4>(OneOf<T1, T2, T3, T4>)
Converts a 4-way OneOf to a 3-way OneOf, returning null when the value is T4.
public static OneOf<T1, T2, T3>? ToThreeWay<T1, T2, T3, T4>(this OneOf<T1, T2, T3, T4> oneOf)
Parameters
oneOfOneOf<T1, T2, T3, T4>
Returns
- OneOf<T1, T2, T3>?
Type Parameters
T1T2T3T4
ToThreeWay<T1, T2, T3, T4>(OneOf<T1, T2, T3, T4>, Func<T4, T1>?, Func<T4, T2>?, Func<T4, T3>?)
Converts a 4-way OneOf to a 3-way OneOf by mapping the T4 case to one of the first three types.
public static OneOf<T1, T2, T3> ToThreeWay<T1, T2, T3, T4>(this OneOf<T1, T2, T3, T4> oneOf, Func<T4, T1>? t4ToT1 = null, Func<T4, T2>? t4ToT2 = null, Func<T4, T3>? t4ToT3 = null)
Parameters
Returns
- OneOf<T1, T2, T3>
Type Parameters
T1T2T3T4
ToTwoWayWithFallback<T1, T2, T3>(OneOf<T1, T2, T3>, T1?, T2?)
Converts a 3-way OneOf to a 2-way OneOf by providing a fallback value for the third type.
public static OneOf<T1, T2> ToTwoWayWithFallback<T1, T2, T3>(this OneOf<T1, T2, T3> oneOf, T1? fallbackT1 = null, T2? fallbackT2 = null) where T1 : class where T2 : class
Parameters
oneOfOneOf<T1, T2, T3>The 3-way OneOf to convert.
fallbackT1T1Fallback T1 value when the OneOf contains T3.
fallbackT2T2Fallback T2 value when the OneOf contains T3.
Returns
- OneOf<T1, T2>
A 2-way OneOf with T3 replaced by the specified fallback.
Type Parameters
T1The first type.
T2The second type.
T3The third type.
Examples
OneOf<Error, Success, Warning> threeWay = new Warning("Deprecated API");
OneOf<Error, Success> twoWay = threeWay.ToTwoWayWithFallback(
fallbackT1: new Error("Warning occurred"),
fallbackT2: new Success("Warning handled")
);
Remarks
This conversion allows you to handle the T3 case by replacing it with a fallback value. You must provide at least one fallback value. If both are provided, fallbackT1 takes precedence.
Exceptions
- ArgumentException
Thrown when both fallback values are null.
ToTwoWay<T1, T2, T3>(OneOf<T1, T2, T3>)
Converts a 3-way OneOf to a 2-way OneOf by filtering out the third type.
public static OneOf<T1, T2>? ToTwoWay<T1, T2, T3>(this OneOf<T1, T2, T3> oneOf)
Parameters
oneOfOneOf<T1, T2, T3>The 3-way OneOf to convert.
Returns
- OneOf<T1, T2>?
A 2-way OneOf if the original contains T1 or T2, null if it contains T3.
Type Parameters
T1The first type.
T2The second type.
T3The third type to filter out.
Examples
OneOf<Error, Success, Warning> threeWay = new Success("Data processed");
OneOf<Error, Success>? twoWay = threeWay.ToTwoWay<Error, Success, Warning>();
if (twoWay.HasValue) { /* process T1/T2 case */ }
Remarks
This conversion filters out the T3 case. If the 3-way OneOf contains T3, the result is null to indicate that the value couldn't be represented in the 2-way type system. This explicit null return makes it clear that data might be lost in the conversion.
ToTwoWay<T1, T2, T3>(OneOf<T1, T2, T3>, Func<T3, T1>?, Func<T3, T2>?)
Converts a 3-way OneOf to a 2-way OneOf by mapping the third type to one of the first two.
public static OneOf<T1, T2> ToTwoWay<T1, T2, T3>(this OneOf<T1, T2, T3> oneOf, Func<T3, T1>? t3ToT1 = null, Func<T3, T2>? t3ToT2 = null)
Parameters
oneOfOneOf<T1, T2, T3>The 3-way OneOf to convert.
t3ToT1Func<T3, T1>Function to convert T3 to T1 when needed.
t3ToT2Func<T3, T2>Function to convert T3 to T2 when needed.
Returns
- OneOf<T1, T2>
A 2-way OneOf with T3 mapped to either T1 or T2.
Type Parameters
T1The first type.
T2The second type.
T3The third type to map.
Examples
OneOf<Error, Success, Warning> threeWay = new Warning("Deprecated API");
OneOf<Error, Success> twoWay = threeWay.ToTwoWay(
t3ToT1: warning => new Error($"Warning: {warning.Message}"),
t3ToT2: warning => new Success($"Warning handled: {warning.Message}")
);
Remarks
This conversion allows you to handle the T3 case by mapping it to either T1 or T2. You must provide at least one mapping function to handle the T3 case. If both functions are provided, t3ToT1 takes precedence.
Exceptions
- ArgumentNullException
Thrown when both mapping functions are null.