我可以禁用“非详尽模式匹配”吗?仅针对 lambda 发出警告?

发布于 2025-01-02 04:43:09 字数 244 浏览 1 评论 0原文

我可以禁用仅针对 lambda 的非详尽模式匹配警告吗?

我一般喜欢这个警告,但不喜欢像这样的实际 lambda 文字:

map (\(x:xs)->...) ls

我认为这段代码非常清楚地表明我希望 ls 的所有值始终至少有一个元素,并且有没有巧妙的方法来处理 lambda 中的错误情况。 (我想我可以将模式匹配移动到 case 语句中,但这会很难看。)

Can I disable the non-exhaustive pattern matches warning only for lambdas?

I like the warning in general, but not for actual lambda literals like this:

map (\(x:xs)->...) ls

I think this code makes it pretty clear that I expect all the values of ls to always have at least one element, and there is no neat way to handle the error case in the lambda. (I guess I could move the pattern match into a case statement, but that would just be ugly.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

有深☉意 2025-01-09 04:43:09

是的,但仅限于 GHC 7.2 及以上版本;通过 -fno-warn-incomplete-uni-patterns(例如,在 Cabal 文件的 ghc-options 字段中,或在 {-# OPTIONS_GHC #-} 中文件顶部的 pragma)。

但是,这也会禁用模式绑定的警告,因此 let Just x = Nothing in x 不会产生警告。 case 语句不受影响。

Yes, but only in GHC 7.2 onwards; pass -fno-warn-incomplete-uni-patterns (e.g. in your Cabal file's ghc-options field, or in an {-# OPTIONS_GHC #-} pragma at the top of your file).

However, this will also disable the warning for pattern bindings, so let Just x = Nothing in x won't produce a warning. case statements are unaffected.

瀞厅☆埖开 2025-01-09 04:43:09

你是否经常遇到这样的情况?恕我直言,这是一种代码味道。我希望看到一些这样的 lambda,并且我非常确定我们可以制作一个更好的版本,它也可以很好地处理空列表。在所有其他情况下,您可能会选择 NonEmpty 列表类型包装器。

Do you have such situations quite often? This is a code smell IMHO. I'd like to see some such lambdas and I am quite sure we can make a better version that also handles empty lists quite fine. And in all other cases you might go for a NonEmpty list type wrapper.

凯凯我们等你回来 2025-01-09 04:43:09

对于map,您可以将其编写为列表理解。

[... | (x:xs) <- ls]

这不会产生任何警告。不过,如果出现空列表,这只会将其过滤掉,而不是抛出异常,这可能会隐藏错误。如果您担心这一点,那么按照 Ingo 建议走类型安全的路线可能是更好的选择。

In the case of map, you could write this as a list comprehension.

[... | (x:xs) <- ls]

This will not produce any warnings. Although, if an empty list does show up, this will simply filter it out rather than throw an exception, which might conceal errors. Going the type safe route as Ingo suggests might be a better option if you're worried about that.

纸短情长 2025-01-09 04:43:09

我会选择 {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} 而不是 {-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}.我建议使用每个文件的方法,而不是将其放入 cabal 文件中,因为不断收到此类警告通常是一种很好的做法。

I would go for {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-} instead of {-# OPTIONS_GHC -fno-warn-incomplete-uni-patterns #-}. And I would recommend using per-file approach instead of putting it into you cabal file as it is generally good practice to keep getting warnings of this kind.

羞稚 2025-01-09 04:43:09

你可以

{-# LANGUAGE LambdaCase #-}
map (\case (x:xs)->...; [] -> error "wut") ls

在“wut”中写下,然后你可以描述为什么这种情况不应该发生。

You could write

{-# LANGUAGE LambdaCase #-}
map (\case (x:xs)->...; [] -> error "wut") ls

In "wut" you could then describe why that should not have happened.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文