I often create structures like: data MyData = MyData { foo :: ..., bar :: ..., .... } That makes 2 of us :-)
and most of the time i do one of two things: 1) read values from the structure, as in: let x = (foo myData) in ... 2) update values in the structure, as in: let myData' = myData { foo = (foo myData)+1 }
1) I've used datatypes with labeled fields mostly to pass around implicit values. If that is your case then there is a way around it. Declare the datatype as
data MyData = MyData { foo_ :: fooType, bar_ :: ..., .... }
and then declare
foo :: (?implicitdata :: MyData)=> fooType foo = foo_ ?yourdata
So when you work in a contex that depends on some implicit data you can just use foo. I've used this *a lot* lately. 2) Yes. My method now is declaring set and apply functions to every field of my data structure. fooAp f ni=ni{foo=f(foo ni)} fooSet x = fooAp (const x)
Only very rarely (usually only during intializization) do I actually put values into the structure that *don't* depend on their previous value. I end up with expresions like:
... myData { foo = (foo myData) + 1 ; bar = (bar myData) ++ "bar" ; ick = (ick myData) ! n ; ... }
Yeap quite ugly isn't it? :-)
I was wondering if there existed any sort of "update" syntax. Obviously Nope, not that I know of.
not real update, but enough to get rid of the "(foo myData)" parts of my epxression which really serve to just clutter up with expression. Perhaps something like:
... myData { foo <- (+1) ; bar <- (++"bar") ; ick <- (!n) ; ... }
Yes looks nice, thought about something like that before too.
or the like, where "x { ... y <- e ... } is translated to "x { ... y = e (y x) ... }" (i only use "<-" because that seems to be the default extension symbol, i guess because we don't want to trample symbols people might actually use.)
Anyway I'd prefer to have some way to 'derive' apply and set functions. Something like
data MyData = MyData { foo :: fooType, bar :: ..., .... } deriving (Set, Apply)
Using the keyword "deriving" would probably be a bad idea though :) The set and apply functions could be derived with a standard postfix or maybe prefix... fooAp or apFoo. Maybe we could introduce sintax to specify it...
deriving (Set with "set", Apply with "ap")
I don't know... I'm just brainstorming right now. Having actual functions is important. I don't think I have to explain why to people in this mailing list :-)
Anyway, does such a thing exist, and, if not, is there any chance it could exist, or is it just syntactic salt to too many people? :) I whish you better luck than I've had so far whenever making posts about this same issue ;)
J.A.