[Haskell-beginners] confused by <-

Chaddaï Fouché chaddai.fouche at gmail.com
Fri Sep 28 21:55:32 CEST 2012


On Thu, Sep 27, 2012 at 11:37 PM, Bryce Verdier <bryceverdier at gmail.com> wrote:
>>>         Left (SomeException _) -> jsonToRepJson $ object ["response" .=
>>> "ERROR: NO DATA FOR ONE REASON OR ANOTHER"]
>
> I was doing that because when I removed it, I got an error similar to this:
>
>     Ambiguous type variable `a0' in the constraints:
>       (Data.String.IsString a0)
>         arising from the literal `"ERROR: "' at playhaven.hs:54:73-81
>       (aeson-0.6.0.2:Data.Aeson.Types.Class.ToJSON a0)
>
> which I was able to remove by throwing a show in there. If you know of a
> better way I would love to know about it.
>

This is probably due to you using OverloadedStrings extension, you can
remove the error while avoiding a superfluous show in two ways :
either you put an explicit type signature on the string or you make
the conversion explicit :

>         Left (SomeException _) -> jsonToRepJson $ object ["response" .= ("ERROR: NO DATA FOR ONE REASON OR ANOTHER" :: Text)]
(or ByteString or String, whatever)

>         Left (SomeException _) -> jsonToRepJson $ object ["response" .= (B.pack "ERROR: NO DATA FOR ONE REASON OR ANOTHER")]
where B.pack is the ByteString pack (you'll need to import it or
import bytestring qualified as B)

Both those solutions avoid extra quotes or other annoying quirks of show.
-- 
Jedaï



More information about the Beginners mailing list