[Haskell-cafe] Alternatives to convoluted record syntax
Henning Thielemann
lemming at henning-thielemann.de
Thu Jul 3 06:13:26 EDT 2008
On Thu, 3 Jul 2008, Dougal Stanton wrote:
> Here's a snippet from the parser for one option (others omitted for clarity):
>
>> options :: [OptDescr (Opts -> Opts)]
>> options =
>> [ Option "b" ["bus"] (ReqArg busNum "NUM") "Bus number"
>> , ...
>> ]
>> where busNum n os = let b = (query os) { queryBusNumber = Just n }
>> in if isBusId n then os { query = b } else os
>
> Variations on that ugliness are repeated four times for other fields.
> Is there an alternative way to change the value of nested fields?
For access to nested record fields I implemented the record-access
package. Unfortunately the field accessors must still be written manually.
http://darcs.haskell.org/record-access/src/Data/Accessor/Example.hs
E.g.:
infix2 :: ((Char, Int), String)
infix2 =
(('b',7),"hallo")$%first^:second^=10
(second^=10) replaces the second member of pair with a value.
(^:) applies that change to the outer record (again a pair).
($%) applies the modifier (first^:second^=10) to a concrete pair.
More information about the Haskell-Cafe
mailing list