<div dir="ltr"><div><div></div>"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.<br><br></div>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.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 13, 2016 at 4:53 PM, Alan Buxton <span dir="ltr"><<a href="mailto:alanbuxton@gmail.com" target="_blank">alanbuxton@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div link="#0563C1" vlink="#954F72" lang="EN-GB"><div><p class="MsoNormal">What am I doing wrong in this admittedly contrived example?<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">The code below will compile. It works as expected, unless I try to do “toMyParam Nothing”. See below:<u></u><u></u></p><p class="MsoNormal">λ: let arr = [P1 3.0, P2 'x']<u></u><u></u></p><p class="MsoNormal">λ: toMyParam False<u></u><u></u></p><p class="MsoNormal">P2 'F'<u></u><u></u></p><p class="MsoNormal">λ: toMyParam (Just 'x')<u></u><u></u></p><p class="MsoNormal">P2 'x'<u></u><u></u></p><p class="MsoNormal">λ: toMyParam Nothing<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><interactive>:38:1:<u></u><u></u></p><p class="MsoNormal"> No instance for (ToMyParam a0) arising from a use of `toMyParam'<u></u><u></u></p><p class="MsoNormal"> The type variable `a0' is ambiguous<u></u><u></u></p><div style="border-top:double windowtext 2.25pt;border-left:none;border-bottom:double windowtext 2.25pt;border-right:none;padding:1.0pt 0cm 1.0pt 0cm"><p class="MsoNormal" style="border:none;padding:0cm"><br>Code below:<u></u><u></u></p></div><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">data MyParam = P1 Double | P2 Char deriving Show<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">class ToMyParam a where<u></u><u></u></p><p class="MsoNormal"> toMyParam :: a -> MyParam<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">instance ToMyParam Bool where<u></u><u></u></p><p class="MsoNormal"> toMyParam False = P2 'F'<u></u><u></u></p><p class="MsoNormal"> toMyParam True = P2 'T'<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">instance ToMyParam Char where<u></u><u></u></p><p class="MsoNormal"> toMyParam = P2<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">instance ToMyParam Double where<u></u><u></u></p><p class="MsoNormal"> toMyParam = P1<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">instance ToMyParam a => ToMyParam (Maybe a) where<u></u><u></u></p><p class="MsoNormal"> toMyParam Nothing = P1 0.0<u></u><u></u></p><p class="MsoNormal"> toMyParam (Just x) = toMyParam x<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal"><u></u> <u></u></p></div></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>