重叠模式匹配
我有以下代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 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 :)您是否打开了
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.