[Haskell-cafe] Fwd: How to make this data type work?

Miguel Mitrofanov miguelimo38 at yandex.ru
Fri Jun 21 11:04:32 CEST 2013


Forgot to reply all, as usual.

-------- Пересылаемое сообщение  --------
21.06.2013, 12:52, "Miguel Mitrofanov" <miguelimo38 at yandex.ru>:

Actually, this is not the real error you should care about. Try removing FromJSON instance completely, and you'll get a lot more. And these are fundamental: you have to decide what "j" to use when serializing. Haskell won't automagically substitute some suitable type for you.

So, that's a classic mismatch: for serializing (ToJSON) you need your "j" type to be known to the AD value (meaning: it should be quantified existentially), but for deserializing you need it to be any type (quantified universally).

All in all, AD seems to be the wrong type.

21.06.2013, 12:18, "Magicloud Magiclouds" <magicloud.magiclouds at gmail.com>:

>  data ActionData = AD { oldData :: (FromJSON j, ToJSON j) => j
>                       , newData :: (FromJSON j, ToJSON j) => j}
>  instance ToJSON ActionData where
>    toJSON (AD o n) = object [ "oldData" .= o
>                             , "newData" .= n ]
>  instance FromJSON ActionData where
>    parseJSON (Object v) = AD
>      <$> v .: "oldData"
>      <*> v .: "newData"
>    parseJSON _ = mzero
>
>  I got when compile:
>      No instance for (FromJSON (forall j. (FromJSON j, ToJSON j) => j))
>        arising from a use of `.:'
>      Possible fix:
>        add an instance declaration for
>        (FromJSON (forall j. (FromJSON j, ToJSON j) => j))
>      In the second argument of `(<$>)', namely `v .: "oldData"'
>      In the first argument of `(<*>)', namely `AD <$> v .: "oldData"'
>      In the expression: AD <$> v .: "oldData" <*> v .: "newData"
>
>  --
>  竹密岂妨流水过
>  山高哪阻野云飞
>
>  And for G+, please use magiclouds#gmail.com.
>  ,
>  _______________________________________________
>  Haskell-Cafe mailing list
>  Haskell-Cafe at haskell.org
>  http://www.haskell.org/mailman/listinfo/haskell-cafe
-------- Завершение пересылаемого сообщения --------



More information about the Haskell-Cafe mailing list