stackoverflowuser
stackoverflowuser

Reputation: 1197

Why does this use Double instead of giving me an ambiguity error?

For the code

class Boomable a where
  boom :: a

instance Boomable Int where
  boom = 100

instance Boomable Double where
  boom = 1.2

Why does

boom + 1

give me 2.2?

Why does it use the Double version instead of giving me an ambiguity error as I expected?

I expected having to do ::Int or ::Double on either boom or 1 for this to work.

Upvotes: 3

Views: 108

Answers (1)

Sander Hahn
Sander Hahn

Reputation: 256

You can enable the warnings using 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

Upvotes: 6

Related Questions