[Haskell-cafe] Problem on using class as type.

Brent Yorgey byorgey at seas.upenn.edu
Mon Oct 3 17:13:45 CEST 2011

On Mon, Oct 03, 2011 at 09:42:59PM +0800, Magicloud Magiclouds wrote:
> Hi,
>   I have a function:
> post :: (ToJson p, FromJson q) => String -> String -> String ->
> Map.Map String p -> IO q
>   Now I'd like to call it like:
> r <- post site token "user.addMedia" (Map.fromList [ ("users", users :: ToJson)
>                                                    , ("medias", medias
> :: ToJson) ])
>   So I got the problem. If I used things like "users :: ToJson", then
> class used as a type error occurred. But if I did not use them, since
> users and medias were actually different types, then fromList failed,
> required the type of medias the same with users.

If users and medias are different types, then you cannot put them in
the same list.  The ToJson class provides a method

  toJson :: ToJson a => a -> Json

so presumably you need to call this method on users and medias, like

  ... ("users", toJson users)
    , ("medias", toJson medias)


More information about the Haskell-Cafe mailing list