louthy/language-ext · error · Exception

Impossible

Error message

Impossible

What it means

Reply<E,S,T>'s Functor instance maps over a Reply value by pattern-matching it to the concrete Reply<E,S,T,A>. Since the K<Reply<E,S,T>,A> wrapper should only ever contain a Reply, any other shape is impossible and throws Exception("Impossible"). Hitting it means a custom/foreign K wrapper was passed in, breaking the trait contract.

Solutions

  1. Only pass genuine Reply<E,S,T,A> values (created by the Megaparsec primitives) into Select/Map
  2. Check for mixed LanguageExt versions (e.g. two LanguageExt.Megaparsec assemblies loaded side by side)
  3. Remove any custom K<Reply,...> implementations or unsafe casts
  4. If caused by a library bug, report it; wrap the Select call in try/catch as a defensive measure

Example fix

// before
var mapped = replyK.Select(x => transform(x)); // replyK may be a foreign wrapper
// after
if (replyK is Reply<E, S, T, int> r)
{
    var mapped = r.Select(x => transform(x));
}
Defensive patterns

Strategy: type-guard

Validate before calling

bool IsValidReply<KT, E, S, T, A>(K<Reply<E,S,T>, A> ma)
    => ma is Reply<E, S, T, A>;

Type guard

static bool TryAsReply<A, B>(K<Reply<E, S, T>, A> ma, out Reply<E, S, T, B> mapped, Func<A, B> f)
{
    if (ma is Reply<E, S, T, A> r) { mapped = new Reply<E, S, T, B>(r.NewState, r.Consumed, f * r.Result); return true; }
    mapped = null!; return false;
}

Try / catch

try
{
    var result = replyK.Select(f);
}
catch (Exception ex) when (ex.Message == "Impossible")
{
    throw new InvalidOperationException(
        "Select/Map received a K<Reply,...> that is not a Reply<E,S,T,A>; check for mixed LanguageExt assemblies or bad casts", ex);
}

Prevention

When it happens

Trigger: Calling Select/Map on a K<Reply<E,S,T>,A> whose runtime value is not a Reply<E,S,T,A> — e.g. a hand-built or wrongly-cast Kind wrapper passed to the functor instance.

Common situations: Mixing parser combinator kinds from different assemblies/versions; unsafe casts of K<> wrappers; custom implementations of the Reply trait family.

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/a514c2af51953b6d. Report an issue: GitHub.

Appendix: source

Thrown at LanguageExt.Megaparsec/Reply/Trait/Reply.TraitImpl.cs:11

using LanguageExt.Traits;

namespace LanguageExt.Megaparsec;

public class Reply<E, S, T> : Functor<Reply<E, S, T>>
{
    static K<Reply<E, S, T>, B> Functor<Reply<E, S, T>>.Map<A, B>(Func<A, B> f, K<Reply<E, S, T>, A> ma) =>
        ma switch
        {
            Reply<E, S, T, A> r => new Reply<E, S, T, B>(r.NewState, r.Consumed, f * r.Result),
            _                   => throw new Exception("Impossible")
        };
}

View on GitHub (pinned to 2f0e362824)