Here's how I understand ocaml refs:
Refs are really just a special case of records, with a little
syntactic sugar. An record is a collection of labeled values, each of
which may or may not be mutable. When a record is passed around in an
ocaml program, in fact only a pointer of some kind is passed around,
so that there is only ever one copy of the actual record data. This is
true even if the record contains mutable fields, so changes to one
copy of a record are visible in all other copies of the record (in
other words, ocaml is a pass-by-reference language, at least with
respect to records).
A ref is just a record with a single mutable value, the data being
stored. It is therefore the equivalent of a const pointer in C++; the
value stored in the record can be modified, but the pointer itself
cannot be modified to point to a different record. Thus, if you want
the full functionality of a C-style pointer, or even a
pointer-to-const, you need a second level of indirection, i.e. a ref
ref, in order to make the pointer itself mutable.
Is that all correct? Is there any better way to get the equivalent of
mutable pointers?
.