在非穷尽模式中更好的例外情况 [英] Better exception for non-exhaustive patterns in case

查看:147
本文介绍了在非穷尽模式中更好的例外情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有办法让GHCi在运行时发现更好的异常消息产生一个不符合功能模式匹配的值?它目前给出了产生非穷尽模式匹配的函数的行号,尽管有时有时需要一轮调试,有时我觉得这是一遍又一遍的做同样的事情。所以在我试图组合一个解决方案之前,我想看看是否还有其他的东西。



一个异常消息,除了给出行号显示什么样的调用尝试做?



这是否可能?

解决方案

尝试在ghci打开警告。例如,通过传递 -W 可以实现通过ghc获取的编译时间警告。您可以通过以下几种方法:

  ghci -fwarn-incomplete-patterns 
/ pre>

Neil Mitchell描述了他如何在他的 .ghci 中设置它。以下是相关摘录:

 :set -fwarn-incomplete-patterns 
/ pre>

您也可以在ghci手动输入,但是每当您启动它时都会很痛苦。以这种方式输入,它仅适用于在提示符下输入的语句,而不适用于使用加载文件:l 。相反,您可以将此评论放在您要对不完整模式发出警告的文件顶部:

  { - #OPTIONS_GHC -fwarn -incomplete-patterns# - } 


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?

It currently gives the line numbers of the function which produced the non-exhaustive pattern match which though helpful at times does require a round of debugging which at times I feel is doing the same set of things over and over. So before I tried to put together a solution I wanted to see if something else exists.

An exception message that in addition to giving the line numbers shows what kind of call it attempted to make?

Is this even possible?

解决方案

Try turning on warnings in ghci. This enables the compile time warnings you can get with ghc by passing -W, for example. You can do this several ways:

ghci -fwarn-incomplete-patterns

Or Neil Mitchell describes how he sets this up in his .ghci. Here is the relevant excerpt:

:set -fwarn-incomplete-patterns

You can manually enter this at ghci as well, but it would be a pain to do so each time you start it. Entered this way, it only works for statements entered at the prompt, not for loading files with :l. Instead you can put this comment at the top of the file you want to warn about incomplete patterns:

{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}

这篇关于在非穷尽模式中更好的例外情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆