在您将其视为重复项之前
我看到至少截至 2018 年 9 月,GHCI 不允许您在本地禁用警告(尽管您可以在整个文件中)。
但也许还有其他方法可以让 GHCI 知道每个案件实际上都在处理?
问题
我有时使用的一个习惯用法是编写一个函数,其中第一个定义测试某些谓词并返回 Left,其他定义考虑操作实际有意义的参数。每当我这样做时,我都会收到“模式匹配并非详尽无遗”的错误,但我确实在检查每个条件。
例子
(有关激发这个玩具示例的真实代码,请参见pExprToHExpr
此处的定义。)
这段代码:
{-# LANGUAGE ViewPatterns #-}
data Cowbell = Cowbell
deriving Show
data Instrument = Rattle
| Drums (Maybe Cowbell)
| Violin
| Oboe
deriving Show
pitched :: Instrument -> Bool
pitched Rattle = False
pitched (Drums Nothing) = False
pitched (Drums (Just Cowbell)) = True
pitched Violin = True
pitched Oboe = True
highestPitch :: Instrument -> Either String Float
highestPitch i@(pitched -> False) =
Left $ "Instrument " ++ show i ++ " has no pitch."
highestPitch (Drums (Just Cowbell)) = Right 800
highestPitch Violin = Right 5000
highestPitch Oboe = Right 2000
生成此错误:
example.hs:19:1: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for ‘highestPitch’:
Patterns not matched:
Rattle
(Drums Nothing)
在其他情况下,我只会细分Instrument
类型:
data Percussive = Rattle | Drums
data Pitched = Violin | Oboe
data Instrument = Percussive Percussive
| Pitched Pitched
但是(在这个假想的物理学中)一组Drums
可能具有最高音调,如果它包含 a Cowbell
,那么它不适合 thePercussive
或Pitched
类型。