I agree that basic (non van Laarhoven) lenses are pretty achievable in OCaml, though I tend to forget about them.
Truly what you want is the "pre-applied" lens or the store comonad
type ('i, 'j) store = { get : 'j; set : 'j -> 'i };;
Then object types can have something like let o = object
val slot = 3
method slot =
{ get = slot
; set = fun slot' -> {< slot = slot' >}
}
end
which has type val o : < slot : ('a, int) store > as 'a
which is kind of a cool pattern. # o#slot.get;;
- : int = 3
# (o#slot.set 10)#slot.get;;
- : int = 10
# o#slot.set o#slot.get;;
- : < slot : ('a, int) store > as 'a = <obj>That is an interesting pattern that essentially does what I need and is much easier to macro expand. Nice job on the pattern!
As a final note, there is an OCaml lens library to help with this, but you're still mostly stuck writing lenses by hand:
https://github.com/avsm/ocaml-lens
Also, Jane Street has fieldslib, which does some interesting things with record types. It doesn't have exactly what I described above, but it does help in many cases and they have some nice macros to help generate getters and setters:
https://github.com/janestreet/fieldslib