Result Pattern & Explicit Errors
EricksonLopez.Result (ADR-001) models expected domain and application failures as explicit readonly struct return values rather than thrown exceptions. It sits at L0 as the central dependency hub for 21 sibling library families (20 published + 1 unpublished local observability).
Result: Non-genericreadonly partial structfor commands or operations that succeed with no return value (Result.Success()) or fail with anError(Result.Failure(error)).Result<TValue>: Genericreadonly structcarrying either aTValueor anError.Result<TValue, TError>(EricksonLopez.Result.Generic): Typed custom error variant when callers require a domain-specific error enum or union instead ofError.Maybe<T>(EricksonLopez.Result.Maybe): Optional presence wrapper that converts cleanly toResult<T>via.ToResult(Error).
The TState overloads on Ensure, Map, Bind, and Tap prevent closure allocations (RESULT004):
using System;using EricksonLopez.Result;
namespace Example.ResultGuide;
public static class AccountWorkflow{ public static Result<decimal> ComputeNetPayout(decimal grossAmount, decimal feeRate) { return Result.Success(grossAmount) .Ensure( static amount => amount > 0m, Error.Validation("Payout.NonPositive", "Gross amount must be positive.")) .Ensure( feeRate, static (rate, _) => rate is >= 0m and < 1m, Error.Validation("Payout.InvalidFeeRate", "Fee rate must be in [0, 1).")) .Map( feeRate, static (rate, amount) => amount * (1m - rate)); }
public static string FormatOutcome(in Result<decimal> outcome) => outcome.Match( onSuccess: static net => $"Net payout: {net:F2}", onFailure: static err => $"Rejected [{err.Code}]: {err.Description}");}When validating user input where all field errors should be surfaced together (rather than failing on the first check), use Result.ValidateAll:
using EricksonLopez.Result;
namespace Example.ResultGuide;
public static class RegistrationRules{ public static Result ValidateInput(string email, int age) => Result.ValidateAll( () => string.IsNullOrWhiteSpace(email) ? Result.Failure(Error.Validation("User.EmailRequired", "Email is required.")) : Result.Success(), () => age < 18 ? Result.Failure(Error.Validation("User.Underage", "Age must be at least 18.")) : Result.Success());}- Read the full EricksonLopez.Result Documentation (including
RESULT001–RESULT013analyzer diagnostics). - Inspect
EricksonLopez.Resultin the Catalog or browse its API Reference.