louthy/language-ext · error · ArgumentException

Cannot create an IterableNE from an empty span

Error message

Cannot create an IterableNE from an empty span

What it means

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.

Solutions

  1. Check ma.IsEmpty (or ma.Length == 0) before calling FromSpan and return an Option/empty iterable instead
  2. Only call FromSpan when you have already verified at least one element exists (e.g. after reading a length prefix)
  3. Slice defensively: if (remaining.Length > 0) FromSpan(remaining) else empty-handling branch
  4. If emptiness is valid, use Iterable<A>.FromSpan (non-NE variant) rather than IterableNE

Example fix

// before
var ne = IterableNE<int>.FromSpan(span); // throws if span is empty
// after
var ne = span.IsEmpty
    ? Option<IterableNE<int>>.None
    : IterableNE<int>.FromSpan(span);
Defensive patterns

Strategy: validation

Validate before calling

// before calling FromSpan
if (span.IsEmpty)
    return Option<IterableNE<A>>.None; // or handle the empty case
var ne = IterableNE<A>.FromSpan(span);

Type guard

static bool IsNonEmptySpan<A>(ReadOnlySpan<A> s) => !s.IsEmpty;

Try / catch

try { var ne = IterableNE<A>.FromSpan(span); }
catch (ArgumentException ex) when (ex.Message.Contains("empty span"))
{
    // empty-input branch: default, Option.None, or domain-specific handling
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at LanguageExt.Core/Immutable Collections/IterableNE/IterableNE.cs:34

/// Non-empty lazy-sequence
/// </summary>
/// <remarks>
/// This always has a Head value and a Tail of length 0 to `n`.   
/// </remarks>
/// <typeparam name="A">Type of the values in the sequence</typeparam>
public record IterableNE<A>(A Head, Iterable<A> Tail) :
    IEnumerable<A>,
    Semigroup<IterableNE<A>>,
    IComparable<IterableNE<A>>,
    IComparisonOperators<IterableNE<A>, IterableNE<A>, bool>,
    IAdditionOperators<IterableNE<A>, IterableNE<A>, IterableNE<A>>,
    K<IterableNE, A>
{
    int? hashCode;

    public static IterableNE<A> FromSpan(ReadOnlySpan<A> ma)
    {
        if (ma.IsEmpty) throw new ArgumentException("Cannot create an IterableNE from an empty span");
        return new IterableNE<A>(ma[0], Iterable<A>.FromSpan(ma.Slice(1)));
    }
    
    [Pure]
    internal bool IsAsync =>
        Tail.IsAsync;
    
    /// <summary>
    /// Number of items in the sequence.
    /// </summary>
    /// <remarks>
    /// NOTE: This will force evaluation of the sequence
    /// </remarks>
    [Pure]
    public int Count() =>
        CountIO().Run();

    /// <summary>

View on GitHub (pinned to 2f0e362824)