louthy/language-ext · error · InvalidOperationException

Guard isn't initialised. It was probably created via new…

Error message

Guard isn't initialised. It was probably created via new Guard() or default(Guard), and so it has no OnFalse handler

What it means

Guard.OnFalse throws InvalidOperationException when the Guard struct was never initialised — created via default(Guard) or new Guard() — so it carries no error handler. Because Guard is a struct, this uninitialised state is possible and is detected only when OnFalse is accessed.

Solutions

  1. Never use default(Guard<E,A>) or new Guard(); always construct via Guard.Get/Checking or the internal constructors with a real onFalse.
  2. Initialise struct fields of type Guard inline or in the constructor.
  3. Treat Guard as disposable compute expression values created from boolean checks, not as long-lived storage.

Example fix

// before
Guard<string, Unit> g = default;
if (!g.Flag) Console.WriteLine(g.OnFalse());
// after
var g = Guard.Get(flag, "predicate failed");
if (!g.Flag) Console.WriteLine(g.OnFalse());
Defensive patterns

Strategy: type-guard

Validate before calling

var initialised = guard.Flag || true; // Flag alone can't detect default; rely on factory presence via construction discipline

Type guard

static bool IsInitialised<E,A>(Guard<E,A> g) => g.Flag || IsConstructed(g); // prefer constructing only via Guard.Get

Try / catch

try { var f = g.OnFalse(); } catch (InvalidOperationException) { /* uninitialised Guard */ }

Prevention

When it happens

Trigger: Evaluating .OnFalse (directly or via Cast/SelectMany on the false branch) of a default(Guard<E,A>) value, e.g. an uninitialized field, array element, or deserialized struct.

Common situations: Storing Guards in structs/collections without initialising them, using Guard as a field without a constructor call, or LINQ query syntax on a default Guard whose Flag is false.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15). Data as JSON: /api/errors/1f00308da00ff71a. Report an issue: GitHub.

Appendix: source

Thrown at LanguageExt.Core/Guard.cs:30

public readonly struct Guard<E, A>
{
    public readonly bool Flag;
    readonly Func<E> onFalse;

    internal Guard(bool flag, Func<E> onFalse) =>
        (Flag, this.onFalse) = (flag, onFalse ?? throw new ArgumentNullException(nameof(onFalse)));

    internal Guard(bool flag, E onFalse)
    {
        if (isnull(onFalse)) throw new ArgumentNullException(nameof(onFalse));
        (Flag, this.onFalse) = (flag, () => onFalse);
    }

    public Guard<E, B> Cast<B>() =>
        new (Flag, OnFalse);
        
    public Func<E> OnFalse =>
        onFalse ?? throw new InvalidOperationException(
            "Guard isn't initialised. It was probably created via new Guard() or default(Guard), and so it has no OnFalse handler");

    public Guard<E, C> SelectMany<C>(Func<E, Guard<E, Unit>> bind, Func<Unit, Unit, C> project) =>
        Flag ? bind(default!).Cast<C>() : Cast<C>();

    public Guard<E, B> Select<B>(Func<B, B> _) =>
        Cast<B>();
}

View on GitHub (pinned to 2f0e362824)