非详尽模式的更好例外,以防万一 [英] Better exception for non-exhaustive patterns in case

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

问题描述

当 GHCi 在运行时发现调用产生的值与函数的模式匹配不匹配时,有没有办法让 GHCi 产生更好的异常消息?

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?

这甚至可能吗?

推荐答案

尝试在 ghci 中打开警告.例如,这将启用您可以通过传递 -W 使用 ghc 获得的编译时警告.您可以通过多种方式执行此操作:

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

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

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

:set -fwarn-incomplete-patterns

您也可以在 ghci 中手动输入此内容,但每次启动时都这样做会很痛苦.以这种方式输入,它只适用于在提示符下输入的语句,不适用于加载带有 :l 的文件.相反,您可以将此注释放在要警告不完整模式的文件顶部:

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天全站免登陆