当我运行这个错误的代码时......
data Person = Adult { pName :: String}
| Kid { pName :: String
, pAge :: Int
} deriving Show
getAge :: Person -> Int
getAge p = pAge p
getName :: Person -> String
getName p = pName p
main :: IO ()
main = do
let p1 = Kid "fred" 5
p2 = Adult "john"
ps = [p1, p2]
names = map getName ps
ages = map getAge ps
putStrLn $ "names: " ++ show names
putStrLn $ "ages: " ++ show ages
...我在 ghci 中得到了这个:
names: ["fred","john"]
ages: [5,* * * Exception: No match in record selector pAge
我知道如何避免这个错误,但我想知道为什么用“ghc -Wall”编译没有警告我这个问题。是否有其他工具可以帮助我防止此类错误?