I recently learned that I can do the following in Haskell:
{-# LANGUAGE DeriveDataTypeable #-}
import Data.Data
data MyRecord = MyRecord
{ field1 :: Int
, field2 :: String
, field3 :: String
} deriving (Show,Eq,Data,Typeable)
main = print $ constrFields (toConstr (MyRecord 5 "Hello" "World"))
This will give me the following:
["field1","field2","field3"]
How can I do the same thing for the values in a record, like this:
["5","Hello","World"]
I'm asking because I'm using Aeson
to take simple JSON like this:
{
"field1":5,
"field2":"Hello",
"field3":"World"
}
And generate Haskell code like this:
field1 :: Int
field1 = 5
field2 :: String
field2 = "Hello"
field3 :: String
field3 = "World"
How can I generically unwrap all the values in a given record in the same way I can unwrap the field names of my records?
MyRecord
? Once you do, you can get at the values by callingfield1
, etc. on the record value.field1
,field2
, etc explicitly in order to access the values. I would like to just generically get all values in the form of a list of strings, just like I was able to do with the field names.