I'm new to Purescript and attempting to use purescript-simple-json to:
- Take some JSON and convert it into a record of
User
- Take some more JSON and convert it into an array of record of
Comment
- Take the result of 2. and put it into the
comments
field of the user record generated from 1.
Below is my attempt at this:
module Test where
import Prelude
import Data.Either (Either)
import Data.Foreign (ForeignError)
import Data.Generic.Rep as Rep
import Data.Generic.Rep.Show (genericShow)
import Data.List.NonEmpty (NonEmptyList)
import Simple.JSON (class ReadForeign, readJSON)
newtype Comment = Comment
{ rating :: Int
, username :: String
}
newtype User = User
{ username :: String
, comments :: Array Comment
}
derive instance repGenericUser :: Rep.Generic User _
instance showUser :: Show User where show = genericShow
derive instance repGenericComment :: Rep.Generic Comment _
instance showComment :: Show Comment where show = genericShow
derive newtype instance rfUser :: ReadForeign User
derive newtype instance rfComment :: ReadForeign Comment
parseU :: String -> Either (NonEmptyList ForeignError) User
parseU xs = readJSON xs
parseC :: String -> Either (NonEmptyList ForeignError) Comment
parseC xs = readJSON xs
addComments :: User -> Array Comment -> User
addComments (User u) cs = u { comments = cs }
However, when I attempt to load this up in the REPL with pulp repl
, I get the following error message:
Compiling Test
Error found:
in module Test
at src/test.purs line 39, column 1 - line 39, column 45
Could not match type
{ comments :: Array Comment
, username :: String
}
with type
User
while checking that type { comments :: Array Comment
| t0
}
is at least as general as type User
while checking that expression $0 { comments = cs
}
has type User
in value declaration addComments
where t0 is an unknown type
I'm not sure how to interpret this error message. I could remove the type signature for addComments
, but the inferred type then becomes the same as the unnamed record in the error message above, which isn't the desired outcome. Any thoughts on what I'm doing wrong? Thanks so much for having a look at this!