为什么这会使用Double而不是给我一个含糊不清的错误?

时间:2014-04-13 11:31:34

标签: haskell

代码

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上执行boom1才能生效。

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