package batteries
Install
Dune Dependency
Authors
Maintainers
Sources
md5=1fd7bddce07cf5d244fc9427f7b5e4d4
sha512=c0f2a0fdc8253e0ea999d8d4c58bfbf32b18d251a2e1d9656bf279de5f01a33e9aabac3af4d95f465f8b671e7711ebd37218043face233340a0c11b08fa62f78
doc/batteries.unthreaded/BatRef/index.html
Module BatRef
Source
Operations on references.
References are mutable values, i.e. "variables" which may actually change value during their life-time, as variables in imperative languages. References can be understood as 1-cell arrays and are typically used to implement imperative algorithms in OCaml.
References are useful but don't abuse them.
!r
returns the current contents of reference r
. Equivalent to fun r -> r.contents
.
r := a
stores the value of a
in reference r
. Equivalent to fun r v -> r.contents <- v
.
Perform an operation on a reference and return the new value of that reference.
For instance, if x
is a reference to 1
, pre x ( ( + ) 1)
returns 2
and sets x
to 2
.
Perform an operation on a reference and return the previous value of that reference.
For instance, if x
is a reference to 1
, post x ( ( + ) 1)
returns 1
and sets x
to 2
.
Increment an integer, return the old value.
Comparable to C or Java's i++
.
Decrement an integer, return the old value.
Comparable to C or Java 's i--
.
Increment an integer, return the new value.
Comparable to C or Java's ++i
.
Increment an integer, return the new value.
Comparable to C or Java's --i
.
Assign a reference temporarily.
protect r v body
sets the value of r
to v
and executes body
. Once body has been executed, whether termination happens as a consequence of regular evaluation or exception, the previous value of r
is restored.
Boilerplate code
Given a printing function for the value in the ref, produce a printing function for the ref.
Example: IO.to_string (Ref.print Int.print) (ref 20) = "20"
Given a comparison function, produce a comparison function for refs of that type.
Example: let a = ref 10 and b = ref 20 in Ref.compare Int.compare a b = -1
Given an ordering function, produce an ordering function for refs of that type.
Example: let a = ref 10 and b = ref 20 in Ref.ord Int.ord a b = Ord.Lt