Better exception for non-exhaustive patterns in case

前端 未结 2 517
陌清茗
陌清茗 2020-11-22 17:03

Is there a way to get GHCi to produce better exception messages when it finds at runtime that a call has produced value that does not match the function\'s pattern matching?

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 17:21

    I recognize that this is something of a non-answer to your question, but my impression is that among veteran Haskell programmers there's a general consensus that non-exhaustive patterns should be avoided in the first place, even to the point of using -Werror to generate errors instead of just warnings.

    I'm not sure how well that works in combination with GHCi, however, especially if you're writing functions at the prompt instead of loading a file--I can imagine it getting in the way more than it helps for working interactively. Running GHCi with the appropriate command-line flags seems to get the desired result for me, though.

    If you want a more drastic solution to non-exhaustive patterns, you could always port Catch to work with modern GHC versions. Heh.

    Beyond that, if you're using non-exhaustive patterns because the function really, truly, should never be called with some values, the missing cases can be filled in with something like error $ "function foo called with ridiculous arguments " ++ show blahBlah, if knowing the invalid arguments would be helpful. Alternately, you could try to rework your code or define more specialized data types such that functions can always do something sensible with any non-bottom argument.

    Otherwise, I think you're stuck with awkward debugging.

提交回复
热议问题