parametric type with constraints - Haskell

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Vincent L

Hi, I'm using a parametric type in my code :

data StudentReport a = StudentReport { nom_prenom :: String, email :: Text, content :: a} deriving (Generic, Show)

However I'd like to be able to serialize such type to JSON sometimes, using aeson ToJSON typeclass.

How can I do that ? I tried

data StudentReport a = StudentReport { nom_prenom :: String, email :: Text, content :: a} deriving (Generic, Show)

instance ToJSON a => ToJSON StudentReport a

But it doesn't work (Expected kind ‘* -> Constraint’, but ‘ToJSON StudentReport’ has kind ‘Constraint’)

I also tried

data ToJSON a => StudentReport a = StudentReport { nom_prenom :: String, email :: Text, content :: a} deriving (Generic, Show)

instance ToJSON StudentReport a

but it doesn't work either ( Expected kind ‘k0 -> Constraint’, but ‘ToJSON StudentReport’ has kind ‘Constraint’)

Georgi Lyubenov // googleson78

the issue in both cases is that you're applying ToJSON to two arguments - StudentReport and a, while ToJSON has only one argument - the type you're serialising

I'm guessing you actually want instance ToJSON a => ToJSON (StudentReport a) - now this is an application of ToJSON to the type StudentReport a

Georgi Lyubenov // googleson78

these are the same rules that apply to terms
fact pred n is fact applied to pred and n, not fact applied to pred n - you want fact (pred n) instead

Vincent L

it works ! thanks !

Vincent L

I forgot to check the "operator precedence" of ToJSON