
On Sat, Sep 11, 2010 at 11:53 AM, Henning Thielemann
Jonathan Geddes schrieb:
I know that record updates is a topic that has become a bit of a dead horse, but here I go anyway:
I find that most of the record updates I read and write take the form
someUpdate :: MyRecord -> MyRecord someUpdate myRecord = myRecord { field1 = f $ field1 myRecord , field2 = g $ field2 myRecord , field3 = h $ filed3 myRecord }
I find myself wishing I could write something more like
someUpdate :: MyRecord -> MyRecord someUpdate myRecord = myRecord { field1 => f , field2 => g , field3 => h }
data-accessor and similar packages may become your friends.
data-accessor allows you to write:
someUpdate = (field1 ^: f) . (field2 ^: g) . (field3 ^: h)
data-accessor is a pretty cool package, but if I understand correctly, your "fields" are not the same as the straight functions you get from defining a record, and cant' be used as regular functions. So you have to create these Data.Accessor.Accessors. Is defining accessors really any better than just writing update functions like the following?
updateField1 :: (Field1Type -> Field1Type) -> MyRecord -> MyRecord updateField1 f x = x{field1 = f $ field1 x} someUpdate = (updateField1 f) . j(updateField2 g) . (updateField3 h)
I understand that there is a package data-accessor-template for generating accessors, but couldn't you use TH for generating updater functions as well? It seems like something as fundamental as record update should have a clean, build-in syntax. Or am I thinking too imperatively? --Jonathan