代码
class Boomable a where
boom :: a
instance Boomable Int where
boom = 100
instance Boomable Double where
boom = 1.2
为什么
boom + 1
给我2.2
?
为什么它使用Double
版本而不是像我预期的那样给出歧义错误?
我希望必须在::Int
或::Double
上执行boom
或1
才能生效。
答案 0 :(得分:6)
您可以使用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