Weak(3)
NAME π
okay so this is Weak(3) from the OCaml library, bestie β the page for Weak: arrays of weak pointers and hash sets of weak pointers. β¨
Module π§
Module Weak β literally the module, and its signature is : sig end. π
Documentation β¨
Module Weak : sig end
Arrays of weak pointers and hash sets of weak pointers. π¦
Low-level functions π
type !'a tβ the type of arrays of weak pointers, aka weak arrays. A weak pointer is a value that the garbage collector may erase whenever the value is not used any more (through normal pointers) by the program. Note that finalisation functions are run before the weak pointers are erased, because the finalisation functions can make values alive again (before4.03the finalisation functions were run after). π
A weak pointer is said to be full if it points to a value, empty if the value was erased by the GC. πΈ
Notes:
-Integersare not allocated and cannot be stored in weak arrays. π-Weakarrays cannot be marshaled usingoutput_valuenor the functions of theMarshalmodule. πval create : int -> 'a tβWeak.create nreturns a new weak array of lengthn. All the pointers are initially empty. RaisesInvalid_argumentifnis not comprised between zero andObj.Ephemeron.max_ephe_length(limits included). β¨val length : 'a t -> intβWeak.length arreturns the length (number of elements) ofar. πval set : 'a t -> int -> 'a option -> unitβWeak.set ar n (Some el)sets thenth cell ofarto be a (full) pointer toel;Weak.set ar n Nonesets thenth cell ofarto empty. RaisesInvalid_argumentifnis not in the range0toWeak.length ar - 1. πval get : 'a t -> int -> 'a optionβWeak.get ar nreturnsNoneif thenth cell ofaris empty,Some x(wherexis the value) if it is full. RaisesInvalid_argumentifnis not in the range0toWeak.length ar - 1. π¦val get_copy : 'a t -> int -> 'a optionβWeak.get_copy ar nreturnsNoneif thenth cell ofaris empty,Some x(wherexis a (shallow) copy of the value) if it is full. In addition to pitfalls with mutable values, the interesting difference withgetis thatget_copydoes not prevent the incremental GC from erasing the value in its current cycle (getmay delay the erasure to the next GC cycle). RaisesInvalid_argumentifnis not in the range0toWeak.length ar - 1. If the element is a custom block it is not copied. πval check : 'a t -> int -> boolβWeak.check ar nreturnstrueif thenth cell ofaris full,falseif it is empty. Note that even ifWeak.check ar nreturnstrue, a subsequentWeak.get ar ncan returnNone. RaisesInvalid_argumentifnis not in the range0toWeak.length ar - 1. βval fill : 'a t -> int -> int -> 'a option -> unitβWeak.fill ar ofs len elsets toelall pointers ofarfromofstoofs + len - 1. RaisesInvalid_argumentifofsandlendo not designate a valid subarray ofar. πval blit : 'a t -> int -> 'a t -> int -> int -> unitβWeak.blit ar1 off1 ar2 off2 lencopieslenweak pointers fromar1(starting atoff1) toar2(starting atoff2). It works correctly even ifar1andar2are the same. RaisesInvalid_argumentifoff1andlendo not designate a valid subarray ofar1, or ifoff2andlendo not designate a valid subarray ofar2. π₯
Weak hash sets π¦
A weak hash set is a hashed set of values. Each value may magically disappear from the set when it is not used by the rest of the program any more. This is normally used to share data structures without inducing memory leaks. Weak hash sets are defined on values from a Hashtbl.HashedType module; the equal relation and hash function are taken from that module. We will say that v is an instance of x if equal x v is true. π
The equal relation must be able to work on a shallow copy of the values and give the same result as with the values themselves. β¨
Unsynchronized accesses β οΈ
Unsynchronized accesses to weak hash sets are a programming error. Unsynchronized accesses to a weak hash set may lead to an invalid weak hash set state. Thus, concurrent accesses to weak hash sets must be synchronized (for instance with a Mutex.t). π«Ά
module type S π
module type S = sig end β the output signature of the functor Weak.Make. π
module Make π
module Make : (H : Hashtbl.HashedType) -> sig end β functor building an implementation of the weak hash set structure. H.equal can't be the physical equality, since only shallow copies of the elements in the set are given to it. π
thatβs the page, bestie! source/version: OCamldoc; date: 2026-06-22; manual slot: Weak(3) π
β¨
π¦ where this page comes from
- arch rolling: ocaml 5.5.0-1 Β· LGPL2.1 AND custom: QPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms