references- Selectors for reading and updating data.

Safe HaskellNone



This module can be used to generate references for record fields. If the field surely exists, a Lens will be generated. If the field may not exist, it will be a Partial lens.

It will have the maximum amount of polymorphism it can create.

If the name of the field starts with "_", the name of the field will be the same with "_" removed. If not, the reference name will be the field name with "_" added te the start.

The following code sample:

data Maybe' a = Just' { _fromJust' :: a }
              | Nothing'
$(makeReferences ''Maybe)

data Tuple a b = Tuple { _fst' :: a, _snd' :: b }
$(makeReferences ''Tuple)

Is equivalent to:

data Maybe' a = Just' { _fromJust' :: a }
              | Nothing'
fromJust' :: Partial (Maybe' a) (Maybe' b) a b
fromJust' = partial (case Just' x -> Right (x, y -> return (Just' y))
                           Nothing' -> Left (return Nothing'))

data Tuple a b = Tuple { _fst' :: a, _snd' :: b }
fst' :: Lens (Tuple a c) (Tuple b c) a b
fst' = lens _fst' (b tup -> tup { _fst' = b })
snd' :: Lens (Tuple a c) (Tuple a d) c d
snd' = lens _snd' (b tup -> tup { _snd' = b })



makeReferences :: Name -> Q [Dec] Source

Creates references for fields of a data structure.

debugTH :: Q [Dec] -> Q [Dec] Source

Shows the generated declarations instead of using them.