小编bee*_*ezz的帖子

在同一记录的其他字段中使用记录字段

我想知道在OCaml中是否有可能在同一记录的另一个字段中使用一个记录字段.

基本上,我有字段功能,我想在其中使用同一记录的其他,值,字段,所以当值更改时,函数将使用新值.

我可以设置功能字段mutable并在创建记录后更新它,例如

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
Run Code Online (Sandbox Code Playgroud)

我想知道是否有可能没有fn字段是可变的并且一气呵成.

ocaml record mutable circular-reference

3
推荐指数
1
解决办法
61
查看次数

标签 统计

circular-reference ×1

mutable ×1

ocaml ×1

record ×1