[Haskell-beginners] Typeclass question

Alan Buxton alanbuxton at gmail.com
Wed Jan 13 21:53:00 UTC 2016


What am I doing wrong in this admittedly contrived example?

 

The code below will compile. It works as expected, unless I try to do
"toMyParam Nothing". See below:

λ: let arr = [P1 3.0, P2 'x']

λ: toMyParam False

P2 'F'

λ: toMyParam (Just 'x')

P2 'x'

λ: toMyParam Nothing

 

<interactive>:38:1:

    No instance for (ToMyParam a0) arising from a use of `toMyParam'

    The type variable `a0' is ambiguous


Code below:

 

 

data MyParam = P1 Double | P2 Char deriving Show

 

class ToMyParam a where

  toMyParam :: a -> MyParam

 

instance ToMyParam Bool where

  toMyParam False = P2 'F'

  toMyParam True = P2 'T'

 

instance ToMyParam Char where

  toMyParam = P2

 

instance ToMyParam Double where

  toMyParam = P1

 

instance ToMyParam a => ToMyParam (Maybe a) where

  toMyParam Nothing = P1 0.0

  toMyParam (Just x) = toMyParam x

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160113/f1763122/attachment.html>


More information about the Beginners mailing list