CSharpEssentials.These
These<TError, TValue> is a three-state union: Left (error only), Right (value only) or Both (a value and an error together). Unlike Result<T>, an error does not mean failure.
dotnet add package CSharpEssentials.These
using CSharpEssentials.These;
using CSharpEssentials.Maybe; // for GetRight()/GetLeft()
using CSharpEssentials.Errors; // for Error type in bridge methods
using CSharpEssentials.ResultPattern; // for Result<T> in bridge methods
Creating These
These<string, int> failure = These<string, int>.Left("error");
These<string, int> success = These<string, int>.Right(42);
These<string, int> partial = These<string, int>.Both("warning", 42);
Checking state
bool isErr = these.IsLeft;
bool isOk = these.IsRight;
bool isBoth = these.IsBoth;
// Safe access via Maybe<T>
Maybe<int> value = these.GetRight(); // None when IsLeft
Maybe<string> error = these.GetLeft(); // None when IsRight
Match
string msg = these.Match(
onLeft: e => $"Error: {e}",
onRight: v => $"Value: {v}",
onBoth: (e, v) => $"Partial: {v} (warning: {e})");
Transforming
// Map transforms the right value; Left passes through unchanged
These<string, int> doubled = these.Map(x => x * 2);
// MapLeft transforms the error; Right passes through unchanged
These<string, int> upper = these.MapLeft(e => e.ToUpper());
// FlatMap chains: Both keeps its original error when the mapper returns Right or Both
These<string, string> chained = these.FlatMap(x => These<string, string>.Right(x.ToString()));
Side effects
// Tap fires on Right or Both
these.Tap(v => logger.LogInformation("Got {Value}", v));
// TapLeft fires on Left or Both
these.TapLeft(e => logger.LogWarning("Warning: {Warning}", e));
Bridge to and from Result
// Result<T> to These<Error, T>: success is Right, failure is Left with the first error only (never Both)
These<Error, int> these = TheseExtensions.FromResult(result);
// These<Error, T> to Result<T> (Both = failure)
Result<int> strict = these.ToResult();
// These<Error, T> to Result<T> (Both = success, discards error side)
Result<int> lenient = these.ToResultLenient();
Partition
var (lefts, rights, boths) = items.Partition();
// lefts: IReadOnlyList<TError>, rights: IReadOnlyList<TValue>, boths: IReadOnlyList<(TError, TValue)>
When to use
| Scenario | Use |
|----------|-----|
| Operation must fully succeed or fail | Result<T> |
| Value may or may not exist | Maybe<T> |
| Partial success with a warning to propagate | These<TError, TValue> |
| Collecting errors while continuing | These<TError, TValue> with Both |
Guidance
- Prefer
Match()over checkingIsLeft/IsRight/IsBothseparately; all three handlers are required. FlatMapfrom Both keeps the original error: mapperRight/BothgivesBoth(originalError, newValue)(the mapper's error is dropped), mapperLeftgives thatLeft.default(These<TError, TValue>)is none of Left, Right or Both:Matchroutes it toonBothwith default values andToResult()throwsInvalidOperationException. Create values throughLeft,RightorBoth.- A
Theseserializes as{"isLeft":...,"isRight":...,"left":...,"right":...}(both flags aretruefor Both). - Use
Result<T>as the general error type;Theseis for partial success.