louthy/language-ext · error · InvalidOperationException

InvalidOperationException

Error message

InvalidOperationException

What it means

SeqConcat.At walks the list of concatenated segments and dereferences each head with ValueUnsafe(); when a segment's head Option is None (corrupt/empty internal segment) it throws InvalidOperationException. This is an internal invariant failure, not a user-input problem.

Solutions

  1. Avoid constructing Seq from raw ISeqInternal segments; use public Seq/SeqStrict/SeqLazy constructors
  2. Update LanguageExt — internal seq DSL invariants were fixed across versions
  3. If hit inside custom DSL code, ensure every segment pushed to the concat list is non-empty
  4. Catch InvalidOperationException defensively at iteration boundaries and rebuild the sequence
Defensive patterns

Strategy: try-catch

Validate before calling

if (seq.IsEmpty || index < 0 || index >= seq.Count) return Option<A>.None;

Try / catch

try { x = seq[index]; }
catch (InvalidOperationException)
{ x = fallback; } // internal invariant failure; rebuild the sequence

Prevention

When it happens

Trigger: Accessing elements of a Seq whose internal segment list contains an empty head — typically after misuse of low-level ISeqInternal APIs or a library bug, surfaced while calling At/indexing.

Common situations: Rare; usually indicates constructing a Seq from an empty ISeqInternal segment in custom DSL code or mixing internal Seq APIs (SeqStrict/SeqLazy/SeqConcat) incorrectly.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at LanguageExt.Core/Immutable Collections/Seq/DSL/SeqConcat.cs:37

        Strict().AsSpan();

    public A this[int index]
    {
        get
        {
            var r = At(index);
            if (r.IsSome) return r.Value!;
            throw new IndexOutOfRangeException();
        }
    }

    public Option<A> At(int index)
    {
        if (index < 0) return default;
        var ms1 = ms;
        while (!ms1.IsEmpty)
        {
            var head = ms1.Head.ValueUnsafe() ?? throw new InvalidOperationException();
            var r    = head.At(index);
            if (r.IsSome) return r;
            index -= head.Count;
            ms1 = ms1.Tail;
        }
        return default;
    }

    public SeqType Type =>
        SeqType.Concat;

    public A Head 
    {
        get 
        {
            foreach (var s in ms)
            {
                foreach (var a in s)

View on GitHub (pinned to 2f0e362824)