haskell - 为什么它使用 Double 而不是给我一个歧义错误?

标签 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 版本而不是像我预期的那样给我一个歧义错误?

我预计必须为此在 boom1 上执行 ::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/

相关文章:

Haskell (ghc) 运行时内存使用情况或者我做错了什么

linux - haskell cabal : Mysterious missing or recursive dependencies

haskell - 即使有类型约束也无法匹配类型错误

haskell - 在 Haskell 中使用别名导入函数

haskell - 将 <q> 和 </q> 标签更改为“特定位置的配对”

haskell - `Data.Map` 上的模式匹配用于实现 `map`

haskell - 类别中初始对象和终止对象之间的差异

haskell 类型

haskell - monad 是否只是组合函数的方式,否则这些函数将无法组合?

haskell - 不使用序列实现mapM