重叠模式匹配

发布于 2024-12-28 13:01:07 字数 523 浏览 5 评论 0原文

我有以下代码:

test :: String -> Bool
test "g" = True
test "global" = True
test _ = False

当我将其加载到 GHCi (7.0.3) 中时,我得到:

Warning: Pattern match(es) are overlapped
         In an equation for `test': test "g" = ...

这是一个错误还是我在这里遗漏了一些东西?

以下保留:

test "" == False
test "g" == True
test "gl" == False
test "global" == True
test "globalx" == False

更新:

我正在使用 {-# LANGUAGE OverloadedStrings #-}

I have the following code:

test :: String -> Bool
test "g" = True
test "global" = True
test _ = False

When I load it into GHCi (7.0.3), I get:

Warning: Pattern match(es) are overlapped
         In an equation for `test': test "g" = ...

Is this a bug or am I missing something here?

The following hold:

test "" == False
test "g" == True
test "gl" == False
test "global" == True
test "globalx" == False

UPDATE:

I am using {-# LANGUAGE OverloadedStrings #-}.

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

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

发布评论

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

评论(2

乖不如嘢 2025-01-04 13:01:07

这是 GHC bug #5117,由使用 OverloadedStrings 扩展引起。它应该在 GHC 7.2 中修复。

作为解决方法,您可以使用 {-# LANGUAGE NoOverloadedStrings #-} 关闭模块的 OverloadedStrings,或使用 {-# OPTIONS_GHC - 关闭警告fno-warn-overlapping-patterns #-}。或者直接忽略它:)

This is GHC bug #5117, arising from the use of the OverloadedStrings extension. It should be fixed in GHC 7.2.

As a workaround, you could turn off OverloadedStrings for the module with {-# LANGUAGE NoOverloadedStrings #-}, or turn off the warning with {-# OPTIONS_GHC -fno-warn-overlapping-patterns #-}. Or just ignore it :)

南城旧梦 2025-01-04 13:01:07

您是否打开了OverloadedStrings?如果我没记错的话,这会导致“虚假”重叠模式警告,因为在这种情况下,不清楚“g”和“global”是否相互排斥。

Have you turned on OverloadedStrings? If I remember correctly, that causes 'spurious' overlapping patterns warnings, because in that case it's not clear that e.g. "g" and "global" are mutually exclusive.

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