I’m generally unsatisfied with writing code like this:
let load_record_field cursor gets geti gett a = function
| 0x01 -> let c, s = gets () in (a.a_record_uuid <- s; `More_record c)
| 0x02 -> let c, s = gets () in (a.a_group <- s; `More_record c)
| 0x03 -> let c, s = gets () in (a.a_title <- s; `More_record c)
| 0x04 -> let c, s = gets () in (a.a_username <- s; `More_record c)
| 0x07 -> let c, t = gett () in (a.a_creation_time <- t; `More_record c)
.
.
.
| 0xFF -> `End_of_record cursor
I’ve minimized the boilerplate, but I was wondering if there was any OCaml magic that would let me completely eliminate it.
This is dead simple: just use a closure to do the setting, and write a function to abstract out the boilerplate
and so on.
You can make this even better if you use a macro package like Jane
Street’s fieldslib. That generates first-class fields, along with
automatically generated setters and getters. This would mean that you
wouldn’t have to construct the closure each time by hand.