The with keyword has several uses.
My favorite involves its usage for giving modified versions of records like:
type blah = { okay : boolean; number : int; }
let blah_instance = { okay = true; number = 0; }
let change_blah_number b num =
{ b with number = num; }
Then calling change_blah_number like:
let new_blah = change_blah_number blah_instance 3 in
...
Yields a nice new_blah that looks like:
new_blah = { okay = true; number = 3; }
You can successfully avoid specifying certain elements--they are automatically copied for you. A brilliant keyword usage for which I've seen little documentation. It helps keep code concise while still showing explicit semantics (i.e., with in this context makes shallow copies of all elements left otherwise undefined).
Comments