I wonder if it is possible in OCaml to use one field of record in the other field in the same record.
Basicaly, I have field with function in which I would like to use also other, values, fields of the same record so when the value change the function will use the new value.
I can do it with setting the function field mutable and update it after the record is created e.g.
type 'a cell =
{ mutable value: 'a
; mutable fn: unit -> 'a }
let create_cell ~(value : 'a) : 'a cell =
let c = {value; fn= (fun () -> value + 42)} in
let _ = c.fn <- (fun () -> c.value + 42) in
c
I was wondering if it is possible without fn field being mutable and in one go.