GHC Generics tools let you inspect constructor name, but what about field names?
Suppose I have a data type
data Foo
= F {f :: Int}
| OO {oo :: String}
| Foo {f :: Int, oo :: String}
And I have the following piece of data
aFoo :: Foo
I can write something like:
ooMay :: Foo -> Maybe String
ooMay f@(Foo {}) = Just (oo f)
ooMay f@(OO {}) = Just (oo f)
ooMay f@(F {}) = Nothing
guarding the accessor oo
by the constructors which I know it is safe to use upon.
Is there a way to write this using Generics? Does something like fieldMay
exist?
ooMay :: Foo -> Maybe String
ooMay f = fieldMay "oo" f
Aucun commentaire:
Enregistrer un commentaire