{"record":{"id":"1f00308da00ff71a","repo":"louthy/language-ext","slug":"guard-isn-t-initialised-it-was-probably-created-via-new","errorCode":null,"errorMessage":"Guard isn't initialised. It was probably created via new Guard() or default(Guard), and so it has no OnFalse handler","messagePattern":"Guard isn't initialised\\. It was probably created via new Guard\\(\\) or default\\(Guard\\), and so it has no OnFalse handler","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Guard.cs","lineNumber":30,"sourceCode":"public readonly struct Guard<E, A>\n{\n    public readonly bool Flag;\n    readonly Func<E> onFalse;\n\n    internal Guard(bool flag, Func<E> onFalse) =>\n        (Flag, this.onFalse) = (flag, onFalse ?? throw new ArgumentNullException(nameof(onFalse)));\n\n    internal Guard(bool flag, E onFalse)\n    {\n        if (isnull(onFalse)) throw new ArgumentNullException(nameof(onFalse));\n        (Flag, this.onFalse) = (flag, () => onFalse);\n    }\n\n    public Guard<E, B> Cast<B>() =>\n        new (Flag, OnFalse);\n        \n    public Func<E> OnFalse =>\n        onFalse ?? throw new InvalidOperationException(\n            \"Guard isn't initialised. It was probably created via new Guard() or default(Guard), and so it has no OnFalse handler\");\n\n    public Guard<E, C> SelectMany<C>(Func<E, Guard<E, Unit>> bind, Func<Unit, Unit, C> project) =>\n        Flag ? bind(default!).Cast<C>() : Cast<C>();\n\n    public Guard<E, B> Select<B>(Func<B, B> _) =>\n        Cast<B>();\n}\n","sourceCodeStart":12,"sourceCodeEnd":39,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Guard.cs#L12-L39","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Never use default(Guard<E,A>) or new Guard(); always construct via Guard.Get/Checking or the internal constructors with a real onFalse.","Initialise struct fields of type Guard inline or in the constructor.","Treat Guard as disposable compute expression values created from boolean checks, not as long-lived storage."],"exampleFix":"// before\nGuard<string, Unit> g = default;\nif (!g.Flag) Console.WriteLine(g.OnFalse());\n// after\nvar g = Guard.Get(flag, \"predicate failed\");\nif (!g.Flag) Console.WriteLine(g.OnFalse());","handlingStrategy":"type-guard","validationCode":"var initialised = guard.Flag || true; // Flag alone can't detect default; rely on factory presence via construction discipline","typeGuard":"static bool IsInitialised<E,A>(Guard<E,A> g) => g.Flag || IsConstructed(g); // prefer constructing only via Guard.Get","tryCatchPattern":"try { var f = g.OnFalse(); } catch (InvalidOperationException) { /* uninitialised Guard */ }","preventionTips":["Never use default(Guard) or new Guard()","Always create Guards via Guard.Get/Checking combinators","Initialise Guard fields at declaration"],"tags":["guard","struct","initialization","invalid-operation"],"backgroundTag":"internal-invariant-violation","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}