Greg Nisbet
Greg Nisbet

Reputation: 6994

OCaml functional update with nested record

Is there a way, possibly using a ppx extension or similar, to use the functional update syntax { record with key = value } with a nested record?

For instance, in the following example program I'm functionally updating only the outermost record, when I really want to target an "inner" one.

type outer = {
  a : float;
  b : inner
}
and inner = {
  c : float;
}

let item = { a = 0.4; b = { c = 0.7 } }

let () = ignore { item with b = { c = 0.8 }

It becomes less convenient if inner has more than one field.

I'd like to be able to write something like the following (strawman syntax):

let () = ignore { item with b.c = 0.8 }

Upvotes: 2

Views: 892

Answers (1)

Jeffrey Scofield
Jeffrey Scofield

Reputation: 66813

You can write this in straight OCaml:

{ item with b = { item.b with c = 0.8 } }

I assume you're using ignore just for the examples; it doesn't make sense to ignore the result of a functional record update.

Upvotes: 1

Related Questions