如果我使用 case 语句将字符串文字与字符串文字进行比较,我会得到预期的行为:如果它们相同 - 它匹配,如果它们不匹配 - 它不匹配。
但是,如果我将字符串文字与作为字符串的常量进行比较,则会收到“模式匹配重叠”警告,并且具有常量的分支始终匹配。
这是一个示例会话:
Prelude> let var1 = "abc"
Prelude> let var2 = "def"
Prelude> case var1 of { var2 -> "Fail"; _ -> "Win" }
<interactive>:1:0:
Warning: Pattern match(es) are overlapped
In a case alternative: _ -> ...
"Fail"
Prelude> case "abc" of { var2 -> "Fail"; _ -> "Win" }
<interactive>:1:0:
Warning: Pattern match(es) are overlapped
In a case alternative: _ -> ...
"Fail"
Prelude> case "abc" of { "def" -> "Fail"; _ -> "Win" }
"Win"
同时,如果行为符合预期:
> Prelude> if var1 == var2 then "Fail" else "Win"
"Win"
这里发生了什么?这种行为有什么意义?