[Haskell-cafe] Alternatives to convoluted record syntax
Misha Aizatulin
avatar at hot.ee
Fri Jul 4 05:29:09 EDT 2008
Hi,
> busNum n
> | (isBusId n) = $(modify 'query) ($(set 'queryBusNumber) (Just n))
> | otherwise = id
>
The solution I am using is creating for each record type @Rec@, and
each of its fields @fieldName :: T@ an updater
updateFieldName :: (T -> T) -> Rec -> Rec
This way you can write
busNum n
| (isBusId n) = updateQuery $ updateQueryBusNumber $ const (Just n)
| otherwise = id
The task of creating updaters can be automated using TH, this is what
the attached library does: all you need is to say
$(genUpdaters ''Opts)
$(genUpdaters ''Query)
Cheers,
Misha
-------------- next part --------------
A non-text attachment was scrubbed...
Name: RecordUpdate.tar.gz
Type: application/x-gzip
Size: 4158 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20080704/49590c54/RecordUpdate.tar.bin
More information about the Haskell-Cafe
mailing list