Safe Haskell | None |
---|---|
Language | Haskell98 |
This module provides an automatic Template Haskell routine to scour data type definitions and generate accessor objects for them automatically.
Documentation
nameMakeLens :: Name -> (String -> Maybe String) -> Q [Dec] Source
nameMakeLens n f
where n
is the name of a data type
declared with data
and f
is a function from names of fields
in that data type to the name of the corresponding accessor. If
f
returns Nothing
, then no accessor is generated for that
field.
makeLenses :: [Name] -> Q [Dec] Source
makeLenses n
where n
is the name of a data type
declared with data
looks through all the declared fields
of the data type, and for each field beginning with an underscore
generates an accessor of the same name without the underscore.
It is "nameMakeLens" n f where f
satisfies
f ('_' : s) = Just s f x = Nothing -- otherwise
For example, given the data type:
data Score = Score { _p1Score :: Int , _p2Score :: Int , rounds :: Int }
makeLenses
will generate the following objects:
p1Score :: Lens Score Int p1Score = lens _p1Score (\x s -> s { _p1Score = x }) p2Score :: Lens Score Int p2Score = lens _p2Score (\x s -> s { _p2Score = x })
It is used with Template Haskell syntax like:
$( makeLenses [''TypeName] )
And will generate accessors when TypeName was declared
using data
or newtype
.