{"record":{"id":"7cd94bb9d3e4d4e1","repo":"louthy/language-ext","slug":"cannot-create-an-iterablene-from-an-empty-span","errorCode":null,"errorMessage":"Cannot create an IterableNE from an empty span","messagePattern":"Cannot create an IterableNE from an empty span","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"LanguageExt.Core/Immutable Collections/IterableNE/IterableNE.cs","lineNumber":34,"sourceCode":"/// Non-empty lazy-sequence\n/// </summary>\n/// <remarks>\n/// This always has a Head value and a Tail of length 0 to `n`.   \n/// </remarks>\n/// <typeparam name=\"A\">Type of the values in the sequence</typeparam>\npublic record IterableNE<A>(A Head, Iterable<A> Tail) :\n    IEnumerable<A>,\n    Semigroup<IterableNE<A>>,\n    IComparable<IterableNE<A>>,\n    IComparisonOperators<IterableNE<A>, IterableNE<A>, bool>,\n    IAdditionOperators<IterableNE<A>, IterableNE<A>, IterableNE<A>>,\n    K<IterableNE, A>\n{\n    int? hashCode;\n\n    public static IterableNE<A> FromSpan(ReadOnlySpan<A> ma)\n    {\n        if (ma.IsEmpty) throw new ArgumentException(\"Cannot create an IterableNE from an empty span\");\n        return new IterableNE<A>(ma[0], Iterable<A>.FromSpan(ma.Slice(1)));\n    }\n    \n    [Pure]\n    internal bool IsAsync =>\n        Tail.IsAsync;\n    \n    /// <summary>\n    /// Number of items in the sequence.\n    /// </summary>\n    /// <remarks>\n    /// NOTE: This will force evaluation of the sequence\n    /// </remarks>\n    [Pure]\n    public int Count() =>\n        CountIO().Run();\n\n    /// <summary>","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Core/Immutable Collections/IterableNE/IterableNE.cs#L16-L52","documentation":"IterableNE<A>.FromSpan builds a non-empty iterable from a ReadOnlySpan<A> by taking ma[0] as head and the remainder as tail. If the span has zero length there is no head element, so the method throws ArgumentException(\"Cannot create an IterableNE from an empty span\") to preserve the type's non-empty invariant.","triggerScenarios":"Calling IterableNE<A>.FromSpan with a ReadOnlySpan<A> where ma.IsEmpty is true (public FromSpan, IterableNE.cs:34) — e.g. span of an empty array, Memory<T>.Span on empty memory, or slicing down to length 0.","commonSituations":"High-performance parsing code slicing buffers that ends with an empty remainder; empty ArraySegment/Memory converted to span; decode loops that call FromSpan on exhausted input; empty stackalloc buffers in hot paths.","solutions":["Check ma.IsEmpty (or ma.Length == 0) before calling FromSpan and return an Option/empty iterable instead","Only call FromSpan when you have already verified at least one element exists (e.g. after reading a length prefix)","Slice defensively: if (remaining.Length > 0) FromSpan(remaining) else empty-handling branch","If emptiness is valid, use Iterable<A>.FromSpan (non-NE variant) rather than IterableNE"],"exampleFix":"// before\nvar ne = IterableNE<int>.FromSpan(span); // throws if span is empty\n// after\nvar ne = span.IsEmpty\n    ? Option<IterableNE<int>>.None\n    : IterableNE<int>.FromSpan(span);","handlingStrategy":"validation","validationCode":"// before calling FromSpan\nif (span.IsEmpty)\n    return Option<IterableNE<A>>.None; // or handle the empty case\nvar ne = IterableNE<A>.FromSpan(span);","typeGuard":"static bool IsNonEmptySpan<A>(ReadOnlySpan<A> s) => !s.IsEmpty;","tryCatchPattern":"try { var ne = IterableNE<A>.FromSpan(span); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"empty span\"))\n{\n    // empty-input branch: default, Option.None, or domain-specific handling\n}","preventionTips":["Check span.Length > 0 / IsEmpty before any FromSpan call","In parsing loops, test the remainder after each Slice before recursing","Avoid FromSpan on spans derived from possibly-empty Memory/ArraySegment","Use Iterable<A>.FromSpan when emptiness is acceptable","Add span-boundary tests (len 0, len 1, len n) to parsing code"],"tags":["empty-span","non-empty-invariant","argument-exception","csharp"],"backgroundTag":"empty-required-field","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"}