generic-deriving 1.6.3 → 1.7.0
raw patch · 2 files changed
+18/−5 lines, 2 files
Files
generic-deriving.cabal view
@@ -1,5 +1,5 @@ name: generic-deriving -version: 1.6.3 +version: 1.7.0 synopsis: Generic programming library for generalised deriving. description:
src/Generics/Deriving/ConNames.hs view
@@ -20,9 +20,9 @@ module Generics.Deriving.ConNames ( - -- * Functionality for retrieving the names of all the possible contructors - -- of a type - ConNames(..), conNames + -- * Functionality for retrieving the names of the possible contructors + -- of a type or the constructor name of a given value + ConNames(..), conNames, conNameOf ) where @@ -30,21 +30,34 @@ class ConNames f where - gconNames :: f a -> [String] + gconNames :: f a -> [String] + gconNameOf :: f a -> String instance (ConNames f, ConNames g) => ConNames (f :+: g) where gconNames (_ :: (f :+: g) a) = gconNames (undefined :: f a) ++ gconNames (undefined :: g a) + gconNameOf (L1 x) = gconNameOf x + gconNameOf (R1 x) = gconNameOf x + instance (ConNames f) => ConNames (D1 c f) where gconNames (_ :: (D1 c f) a) = gconNames (undefined :: f a) + + gconNameOf (M1 x) = gconNameOf x instance (Constructor c) => ConNames (C1 c f) where gconNames x = [conName x] + gconNameOf x = conName x + + -- We should never need any other instances. -- | Return the name of all the constructors of the type of the given term. conNames :: (Generic a, ConNames (Rep a)) => a -> [String] conNames x = gconNames (undefined `asTypeOf` (from x)) + +-- | Return the name of the constructor of the given term +conNameOf :: (ConNames (Rep a), Generic a) => a -> String +conNameOf x = gconNameOf (from x)