Csharpessentials these

From senrecep/CSharpEssentials · View interactive version

Csharpessentials these is an AI agent skill from the GitHub repository senrecep/CSharpEssentials. Use when modeling partial success. These<TError, TValue> holds Left (error only), Right (value only), or Both (error + value), enabling scenarios where a result can partially succeed while carrying warnings. Use FromResult to bridge from Result<T>, ToResult/ToResultLenient to bridge back, and Partit

How to use this skill

Skill file

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

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.