You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently the only way to get record field unpacking when using a sum-of-records is to avoid intermediate types and use the horrible misfeature that is partial record fields.
data Vehicle = Car { make :: String, speed :: Int } | Bicycle { brand :: String, gears :: Int }
deriving (Generic, ToJSON)
encode $ Car { make = "MINI", speed = 150 }
> {"tag":"Car","make":"MINI","speed":150}
vs:
data Vehicle = VehicleCar Car | VehicleBicycle Bicycle
deriving (Generic, ToJSON)
data Car = Car { make :: String, speed :: Int }
deriving (Generic, ToJSON)
data Bicycle = Bicycle { brand :: String, gears :: Int }
deriving (Generic, ToJSON)
encode . VehicleCar $ Car { make = "MINI", speed = 150 }
> {"tag":"VehicleCar","contents":{"speed":150,"make":"MINI"}}
If TaggedObject had an unpack :: Bool parameter users would be able to decide if they want unpacking or not, using contentsFieldName when it's False. Alternatively you could make contentsFieldName a Maybe String, this would just prevent any kind of error recovery if one of the sub-objects serializes to something other than a record, although it's unclear if such error recovery would be desirable anyway.
The text was updated successfully, but these errors were encountered:
Currently the only way to get record field unpacking when using a sum-of-records is to avoid intermediate types and use the horrible misfeature that is partial record fields.
vs:
If
TaggedObject
had anunpack :: Bool
parameter users would be able to decide if they want unpacking or not, usingcontentsFieldName
when it'sFalse
. Alternatively you could makecontentsFieldName
aMaybe String
, this would just prevent any kind of error recovery if one of the sub-objects serializes to something other than a record, although it's unclear if such error recovery would be desirable anyway.The text was updated successfully, but these errors were encountered: