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
- 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
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
- 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
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
- Can't create an IterableNE from an empty sequence
- Key doesn't exist in map
- Refs can only be written to from within a `sync` transaction
- Refs can only commute from within a transaction
- Transaction not running
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)