查看代码
class Boomable a where
boom :: a
instance Boomable Int where
boom = 100
instance Boomable Double where
boom = 1.2
为什么
boom + 1
给我2.2
?
为什么它使用 Double
版本而不是像我预期的那样给我一个歧义错误?
我预计必须为此在 boom
或 1
上执行 ::Int
或 ::Double
去工作。
最佳答案
您可以使用 ghci -Wall
启用警告:
$ ghci -Wall
Prelude> :set +m
Prelude> class Boomable a where
Prelude| boom :: a
Prelude|
Prelude> instance Boomable Int where
Prelude| boom = 100
Prelude|
Prelude> instance Boomable Double where
Prelude| boom = 1.2
Prelude|
Prelude> boom + 1
<interactive>:12:6: Warning:
Defaulting the following constraint(s) to type `Double'
(Num a0) arising from a use of `+' at <interactive>:12:6
(Boomable a0) arising from a use of `boom' at <interactive>:12:1-4
In the expression: boom + 1
In an equation for `it': it = boom + 1
<interactive>:12:6: Warning:
Defaulting the following constraint(s) to type `Double'
(Num a0) arising from a use of `+' at <interactive>:12:6
(Show a0) arising from a use of `print' at <interactive>:12:1-8
(Boomable a0) arising from a use of `boom' at <interactive>:12:1-4
In the expression: boom + 1
In an equation for `it': it = boom + 1
2.2
关于haskell - 为什么它使用 Double 而不是给我一个歧义错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23042199/