{"record":{"id":"1bbec0fdd57ba688","repo":"louthy/language-ext","slug":"nil-iterator-has-no-head","errorCode":null,"errorMessage":"Nil iterator has no head","messagePattern":"Nil iterator has no head","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/Iterator/DSL/Iterator.Nil.cs","lineNumber":35,"sourceCode":"    K<Iterator, A>\n{\n    /// <summary>\n    /// Nil iterator case\n    ///\n    /// The end of the sequence.\n    /// </summary>\n    public sealed class Nil : Iterator<A>\n    {\n        public static readonly Iterator<A> Default = new Nil();\n\n        public override string ToString() => \n            \"Nil\";\n\n        /// <summary>\n        /// Head element\n        /// </summary>\n        public override A Head =>\n            throw new InvalidOperationException(\"Nil iterator has no head\");\n\n        /// <summary>\n        /// Tail of the sequence\n        /// </summary>\n        public override Iterator<A> Tail =>\n            this;\n\n        /// <summary>\n        /// Return true if there are no elements in the sequence.\n        /// </summary>\n        public override bool IsEmpty =>\n            true;\n\n        /// <summary>\n        /// Clone the iterator so that we can consume it without having the head item referenced.\n        /// This will stop any GC pressure.\n        /// </summary>\n        public override Iterator<A> Clone() =>","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/Iterator/DSL/Iterator.Nil.cs#L17-L53","documentation":"The Nil (empty) case of the Iterator discriminated union has no head element, so accessing its Head property throws InvalidOperationException. This is the iterator-DSL equivalent of calling List.head on an empty list.","triggerScenarios":"Accessing `.Head` on an Iterator<A> that is the Nil case — typically obtained by walking Tail off a single-element iterator, or constructing Iterator.Nil<A>() and reading Head directly.","commonSituations":"Hand-rolled recursive iteration that recurses past the last element (checking Head before IsEmpty); assuming an iterator is non-empty; off-by-one Tail traversal in pattern-matching-style code.","solutions":["Check `iter.IsEmpty` (or match on Nil/Cons style) before reading Head.","Use the library's Fold/Map/Filter combinators instead of manual Head/Tail recursion.","If a value must exist, guard with a Try/Optional wrapper such as `iter.IsEmpty ? Prelude.None : Some(iter.Head)`."],"exampleFix":"// before\nvar head = iter.Head;\n// after\nvar head = iter.IsEmpty ? Prelude.None : Prelude.Some(iter.Head);","handlingStrategy":"validation","validationCode":"if (iter.IsEmpty) throw new InvalidOperationException(\"Cannot take Head of an empty iterator\");\nvar head = iter.Head;","typeGuard":"static Option<A> TryHead<A>(Iterator<A> it) => it.IsEmpty ? Prelude.None : Prelude.Some(it.Head);","tryCatchPattern":"try { head = iter.Head; }\ncatch (InvalidOperationException) { head = Option<A>.None; }","preventionTips":["Always test IsEmpty before Head/Tail recursion on Iterator.","Stop recursion when Tail returns the iterator itself (Nil.Tail == this).","Prefer library combinators (Fold, Map, Filter) over manual Head/Tail traversal.","Return Option<A> at API boundaries instead of raw Head."],"tags":["csharp","languageext","empty-sequence","iterator"],"backgroundTag":"empty-result-set","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"}