"toMyParam Nothing" is of type "ToMyParam a => Maybe a", but because you used Nothing and not Just (something of type a) doesn't know what the "a" is and so you have to tell it.  The fact that you don't reference the a in the Nothing case does not exempt you from this requirement.

toMyParam (Nothing :: Maybe Char) will fix your error.  I think there might be a way to get rid of this ambiguity via a type extension but I'm not entirely sure.

On Wed, Jan 13, 2016 at 4:53 PM, Alan Buxton <alanbuxton@gmail.com> wrote:

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

 

 


_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners