Question

I'm trying to add automagical json parsing to Data.Vinyl

Here is an instance for FromJSON for records with exactly one element.

It almost works, but I can't satisfy the KnownSymbol constraint, it seems to auto generate a new type variable on me.

instance (KnownSymbol sym, FromJSON a) => FromJSON (PlainRec '[ sym ::: a ]) where
    parseJSON (Object v) = (field =:) <$> (v .: json_name)
        where field = Field :: (sym ::: a)
              json_name = T.pack $ show field

The error is

Could not deduce (KnownSymbol sym0) arising from a use of ‛show’
from the context (KnownSymbol sym, FromJSON a)

More context http://lpaste.net/101005

If I replace all instances of sym with "name", it works, and runs and it is wonderful. Now, I could use template Haskell to generate all the instances ahead of time, since I have a closed list of field names that I'll actually use, but that seems like such a shame. I know next to nothing about Data.Proxy, having just seen in used to define the show instance for the records of Data.Proxy.

Was it helpful?

Solution

You just have to enable ScopedTypeVariables.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top