{"record":{"id":"a514c2af51953b6d","repo":"louthy/language-ext","slug":"impossible","errorCode":null,"errorMessage":"Impossible","messagePattern":"Impossible","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LanguageExt.Megaparsec/Reply/Trait/Reply.TraitImpl.cs","lineNumber":11,"sourceCode":"using LanguageExt.Traits;\n\nnamespace LanguageExt.Megaparsec;\n\npublic class Reply<E, S, T> : Functor<Reply<E, S, T>>\n{\n    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) =>\n        ma switch\n        {\n            Reply<E, S, T, A> r => new Reply<E, S, T, B>(r.NewState, r.Consumed, f * r.Result),\n            _                   => throw new Exception(\"Impossible\")\n        };\n}\n","sourceCodeStart":1,"sourceCodeEnd":14,"githubUrl":"https://github.com/louthy/language-ext/blob/2f0e3628242889774d4141960a35671a0280051f/LanguageExt.Megaparsec/Reply/Trait/Reply.TraitImpl.cs#L1-L14","documentation":"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.","triggerScenarios":"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.","commonSituations":"Mixing parser combinator kinds from different assemblies/versions; unsafe casts of K<> wrappers; custom implementations of the Reply trait family.","solutions":["Only pass genuine Reply<E,S,T,A> values (created by the Megaparsec primitives) into Select/Map","Check for mixed LanguageExt versions (e.g. two LanguageExt.Megaparsec assemblies loaded side by side)","Remove any custom K<Reply,...> implementations or unsafe casts","If caused by a library bug, report it; wrap the Select call in try/catch as a defensive measure"],"exampleFix":"// before\nvar mapped = replyK.Select(x => transform(x)); // replyK may be a foreign wrapper\n// after\nif (replyK is Reply<E, S, T, int> r)\n{\n    var mapped = r.Select(x => transform(x));\n}","handlingStrategy":"type-guard","validationCode":"bool IsValidReply<KT, E, S, T, A>(K<Reply<E,S,T>, A> ma)\n    => ma is Reply<E, S, T, A>;","typeGuard":"static bool TryAsReply<A, B>(K<Reply<E, S, T>, A> ma, out Reply<E, S, T, B> mapped, Func<A, B> f)\n{\n    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; }\n    mapped = null!; return false;\n}","tryCatchPattern":"try\n{\n    var result = replyK.Select(f);\n}\ncatch (Exception ex) when (ex.Message == \"Impossible\")\n{\n    throw new InvalidOperationException(\n        \"Select/Map received a K<Reply,...> that is not a Reply<E,S,T,A>; check for mixed LanguageExt assemblies or bad casts\", ex);\n}","preventionTips":["Never construct K<Reply<E,S,T>,A> wrappers manually; use Megaparsec primitives","Ensure only one version of LanguageExt.Megaparsec is loaded in the process","Prefer concrete Reply<E,S,T,A> values over kind-projected K<> when writing combinators"],"tags":["megaparsec","parser","functor","pattern-matching","internal-invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"2f0e3628242889774d4141960a35671a0280051f","analyzedAt":"2026-09-15T03:31:55.716Z","contentChangedAt":"2026-09-15T03:31:55.716Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}