代码仍然编译,尽管值构造函数没有给出参数。

时间:2016-05-24 09:12:01

标签: haskell

numberOfChildren

正如您所看到的,data World = Green Double | Red Double deriving Show ws = [Green, Red] Green应该分别给出一个参数。我希望GHC会在编译时抛出错误,但事实并非如此,编译代码。这是正常的吗?

1 个答案:

答案 0 :(得分:6)

我希望MathematicalOrchid不介意(如果你发表评论我删除了)

这不是一个错误 - 编译器正在按照你的要求进行操作 - 当你输入

时,typechecker会推断出一个函数列表ws :: [Double -> World]
ws = [Green, Red]

这就是为什么给出预期的签名是个好主意,这样编译器/类型检查器会让你意识到你的错误

ws :: [World]
ws = [Green, Red] -- error

会给你

YourFile.hs:...: Couldn't match expected type `World' …
                with actual type `Double -> World'
    Probable cause: `Green' is applied to too few arguments
    In the expression: Green
    In the expression: [Green, Red]
YourFile.hs:...: Couldn't match expected type `World' …
                with actual type `Double -> World'
    Probable cause: `Red' is applied to too few arguments
    In the expression: Red
    In the expression: [Green, Red]