Wow! And to think I was thinking of asking the OCaml devs to implement it like F# did!
This reminds me, I once cooked up this usage with lenses:
#require "lens.ppx_deriving";;
module Address = struct
type t = { street : string; city : string }
[@@deriving lens]
end
module Person = struct
type t = { id : string; name : string; address : Address.t }
[@@deriving lens]
end
let ( /: ) first second = Lens.compose second first
let ( .:[] ) obj lens = lens.Lens.get obj
let ( .:[]<- ) obj lens value = lens.Lens.set value obj
let bob = {
Person.id = "1";
name = "Bob";
address = {
Address.street = "1 Way St";
city = "Cityville";
};
}
let bob2 = bob.:[Person.address /: Address.street] <- "Rodeo Dr"
==>
val bob2 : Person.t = {
Person.id = "1";
name = "Bob";
address = {
Address.street = "Rodeo Dr";
city = "Cityville";
};
}
EDIT: with a little difflist magic, this could probably become just:
let bob2 = bob.:[[Person.address; Address.street]] <- "Rodeo Dr"