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 k being true does not promise find h k will 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 (or Not_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? (true doesn't guarantee a later find, 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 as Hashtbl.SeededS.stats but 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, and clean / stats_alive are 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