Ephemeron.S(3)
π Ephemeron.S(3) β the OCaml library glow-up β¨
ok so picture this: you're deep in the OCaml library manual, page Ephemeron.S(3), and you land on this baddie. Welcome bestie, let me walk you through it π«Ά
πΈ NAME
Ephemeron.S β literally the output signature of the functors Ephemeron.K1.Make and Ephemeron.K2.Make. that's it, that's the vibe π
π Module type
Module type Ephemeron.S
π Documentation
π¦ Module type S
module type S
= sig end
So what is this? It's the output signature of the functors Ephemeron.K1.Make and Ephemeron.K2.Make β¨ These hash tables are weak in the keys β meaning: if all the keys of a binding are alive, the binding is kept. But the second one of the keys of that binding is dead? the binding gets removed. gone. deleted. no cap π
It proposes the same interface as your usual hash table, which sounds slay, BUT β and this is the important part, like, listen to me β since the bindings are weak, even if mem h k is true, a subsequent find h k may raise Not_found, because the garbage collector can run between the two. π the GC is the main character here and it does not care about your plans.
π¨ reminder bestie:mem h kbeingtruedoes not promisefind h kwill succeed. the GC may have slayed that binding in between. not a bug, just the ephemeron lifestyle.
π The types
type keyβ the key type. iconic, mysterious, doesn't elaborate πtype !'a tβ the table type itself, with that!variance annotation sitting there looking all fancy β
π₯ The values, one by one
val create : int -> 'a tβ make a new table, you know the drill πval clear : 'a t -> unitβ wipe the whole table π§Ήval reset : 'a t -> unitβ reset it, another clean slate energy β¨val copy : 'a t -> 'a tβ duplicate the table, twin moment π¦val add : 'a t -> key -> 'a -> unitβ add a binding, put it in πval remove : 'a t -> key -> unitβ take that binding out πval find : 'a t -> key -> 'aβ grab the value for a key (orNot_found, see the warning above π)val find_opt : 'a t -> key -> 'a optionβ same but wrapped in an option, the gentler version π«Άval find_all : 'a t -> key -> 'a listβ all the values for that key, in a list β¨val replace : 'a t -> key -> 'a -> unitβ swap the binding, out with the old, in with the new πval mem : 'a t -> key -> boolβ is this key in there? (truedoesn't guarantee a laterfind, remember π)val length : 'a t -> intβ how many bindings, a number, iconic πval stats : 'a t -> Hashtbl.statisticsβ hashtable stats, the receipts πval add_seq : 'a t -> (key * 'a) Seq.t -> unitβ add a whole sequence of bindings, bulk slay π₯val replace_seq : 'a t -> (key * 'a) Seq.t -> unitβ replace from a sequence, same energy πval of_seq : (key * 'a) Seq.t -> 'a tβ build a whole fresh table from a sequence, chef's kiss πval clean : 'a t -> unitβ remove all dead bindings. Done automatically during automatic resizing, so honestly it does this itself, but you can call it πval stats_alive : 'a t -> Hashtbl.statisticsβ same asHashtbl.SeededS.statsbut only count the alive bindings β like a stats call that did a vibe check first
π«Ά quick recap: weak keys, same interface as a normal hash table, GC can delete your bindings mid-thought, andclean/stats_aliveare your friends for dealing with the fallout.
that's the whole page bestie, every type, every val, every warning β nothing skipped, we ate and left no crumbs π
sign-off π: this lil page came from OCamldoc, dated 2026-06-22, on the Ephemeron.S(3) page in section 3 of the manual. mwah, go write some ephemerons π
π¦ 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