on master help sign in
Reduce duplication, simplify CPP macros
commit1f569dd38fc8545be9d31986df8778358e15d4a2 authorBryan O'Sullivan <bos@serpentine.com> dateMon 10 Mar 2014, 21:53 (13 years ago) parentc8ea986
1 file changed+11−10
Data/Aeson/TH.hs+11−10
Data/Aeson/TH.hs+11−10
@@ -165,11 +165,7 @@ deriveToJSON opts name =
165165 where
166166 fromCons :: [TyVarBndr] -> [Con] -> Q Dec
167167 fromCons tvbs cons =
168−#if __GLASGOW_HASKELL__ >= 709
169− instanceD (return $ map (\t -> AppT (ConT ''ToJSON) (VarT t)) typeNames)
170−#else
171− instanceD (return $ map (\t -> ClassP ''ToJSON [VarT t]) typeNames)
172−#endif
168+ instanceD (applyCon ''ToJSON typeNames)
173169 (classType `appT` instanceType)
174170 [ funD 'toJSON
175171 [ clause []
@@ -376,11 +372,7 @@ deriveFromJSON opts name =
376372 where
377373 fromCons :: [TyVarBndr] -> [Con] -> Q Dec
378374 fromCons tvbs cons =
379−#if __GLASGOW_HASKELL__ >= 709
380− instanceD (return $ map (\t -> AppT (ConT ''FromJSON) (VarT t)) typeNames)
381−#else
382− instanceD (return $ map (\t -> ClassP ''FromJSON [VarT t]) typeNames)
383−#endif
375+ instanceD (applyCon ''FromJSON typeNames)
384376 (classType `appT` instanceType)
385377 [ funD 'parseJSON
386378 [ clause []
@@ -880,3 +872,12 @@ valueConName (String _) = "String"
880872valueConName (Number _) = "Number"
881873valueConName (Bool _) = "Boolean"
882874valueConName Null = "Null"
875+
876+applyCon :: Name -> [Name] -> Q [Pred]
877+applyCon con typeNames = return (map apply typeNames)
878+ where apply t =
879+#if __GLASGOW_HASKELL__ >= 709
880+ AppT (ConT con) (VarT t)
881+#else
882+ ClassP con [VarT t]
883+#endif