louthy/language-ext · error · NotSupportedException

FoldBack* is currently not supported for IterableNE

Error message

FoldBack* is currently not supported for IterableNE

What it means

IterableNE (non-empty iterable) implements Foldable but its FoldBackWhile (and other FoldBack*) trait members are not implemented; they unconditionally throw NotSupportedException. This is a deliberate library gap, not a bug in your code — backward folding over IterableNE has no implementation yet.

Solutions

  1. Use the forward-fold equivalents instead: FoldWhile / Fold on the same IterableNE value.
  2. Convert to a structure with full Foldable support first, e.g. `xs.ToLst()` or `xs.ToArr()`, then fold back over that.
  3. If you truly need backward folding in place, accumulate via Fold into a reversed structure or supply your own fold-back helper over ToArr().

Example fix

// before
var sum = xs.FoldBackWhile(s => a => s + a, x => x < 10, 0);
// after
var sum = xs.FoldWhile(s => a => s + a, x => x < 10, 0);
Defensive patterns

Strategy: fallback

Validate before calling

// Feature-check before use (NotSupportedException is thrown unconditionally for FoldBack* on IterableNE)
var canFoldBack = xs is not IterableNE<A>; // for IterableNE use FoldWhile or convert first

Type guard

static bool SupportsFoldBack<A>(K<IterableNE, A> _) => false; // FoldBack* is never supported on IterableNE

Try / catch

try { result = xs.FoldBackWhile(f, pred, state); }
catch (NotSupportedException) { result = xs.FoldWhile(f, pred, state); }

Prevention

When it happens

Trigger: Calling FoldBackWhile, FoldBack, FoldBackT or any FoldBack* operation with the Foldable<IterableNE> instance, e.g. `xs.FoldBackWhile(f, pred, state)` where xs is an IterableNE<A>.

Common situations: Generic foldable code written against IEnumerable or Lst that gets reused on IterableNE; switching a data structure from Lst/Seq to IterableNE in generic fold pipelines; expecting feature parity between forward folds and backward folds.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at LanguageExt.Core/Immutable Collections/IterableNE/Trait/IterableNE.TraitImpl.cs:54

    static K<IterableNE, B> Applicative<IterableNE>.Apply<A, B>(K<IterableNE, Func<A, B>> mf, Memo<IterableNE, A> ma) =>
        mf >> ma.Map;

    static K<IterableNE, A> SemigroupK<IterableNE>.Combine<A>(K<IterableNE, A> ma, K<IterableNE, A> mb) =>
        ma.As().Concat(mb.As());
    
    static S Foldable<IterableNE>.FoldWhile<A, S>(
        Func<A, Func<S, S>> f,
        Func<(S State, A Value), bool> predicate,
        S state,
        K<IterableNE, A> ta) =>
        ta.As().FoldWhile(f, predicate, state);
    
    static S Foldable<IterableNE>.FoldBackWhile<A, S>(
        Func<S, Func<A, S>> f, 
        Func<(S State, A Value), bool> predicate, 
        S state, 
        K<IterableNE, A> ta) =>
        throw new NotSupportedException("FoldBack* is currently not supported for IterableNE");
    
    static Arr<A> Foldable<IterableNE>.ToArr<A>(K<IterableNE, A> ta) =>
        new(ta.As());

    static Lst<A> Foldable<IterableNE>.ToLst<A>(K<IterableNE, A> ta) =>
        new(ta.As());

    static Iterable<A> Foldable<IterableNE>.ToIterable<A>(K<IterableNE, A> ta) =>
        ta.As().AsIterable();
    
    static Seq<A> Foldable<IterableNE>.ToSeq<A>(K<IterableNE, A> ta) =>
        new(ta.As());

    static K<Seq, A> Natural<IterableNE, Seq>.Transform<A>(K<IterableNE, A> fa) => 
        toSeq(fa.As());

    static K<Arr, A> Natural<IterableNE, Arr>.Transform<A>(K<IterableNE, A> fa) => 
        toArray(fa.As());

View on GitHub (pinned to 2f0e362824)