MoreLabels.Hashtbl.HashedType(3)
π MoreLabels.Hashtbl.HashedType(3) β the OCaml library page, bestie edition
okay so this is a page from the OCaml library manual, and it's literally the whole MoreLabels.Hashtbl.HashedType(3) moment. it's not a big dramatic page, it's a module type, but it's foundational β the vibe check that every hashtable key has to pass before it's allowed in. iykyk β¨
πΈ NAME πΈ
MoreLabels.Hashtbl.HashedType β the input signature of the functor MoreLabels.Hashtbl.Make. that's it, that's the job, and it eats. π
π Module type π
so, the whole thing, the entire module type, is literally this:
Module type MoreLabels.Hashtbl.HashedType
she's a module type, she's small, she's powerful, she's main character energy. π«Ά
β¨ Documentation β¨
Module type HashedType
= sig end
okay yes it looks empty from the outside and that's the joke β the meat is all in the members below. it's the input signature of the functor MoreLabels.Hashtbl.Make β so like, before you can call that functor, you gotta hand it a module matching this shape. no cap, that's the entire contract. π
π type t
the type of the hashtable keys. your keys, your type, your world, bestie. π¦
π§ val equal : t -> t -> bool
the equality predicate used to compare keys. yes she's back, yes she compares keys, yes you have to actually mean it.
β val hash : t -> int
a hashing function on keys. now listen β this is the part where you can NOT be sloppy:
- it must be such that if two keys are equal according to
equal, then they have identical hash values as computed byhash. periodt. if yourequalsays "same" but yourhashsays "different", the whole table goes feral and it's giving chaos. π - the hash value of a key should remain constant as long as the key is in the table. in particular, if the hash function depends on mutable key data, then that data must not be mutated while the key is in the table. do not touch the key. leave her alone. she is busy being hashed. π₯
- similarly, as the hash function may be called while the table itself is being modified, it should avoid accessing the table as part of its computation. like, don't reach into the bowl while the bowl is being stirred. that's not a good look. π
fr fr: those two caveats are real. ignore them and you get the undefined-behaviour girlies showing up uninvited. π©·
π Examples: suitable (equal, hash) pairs π
for arbitrary key types, the iconic combos include:
((=), MoreLabels.Hashtbl.HashedType.hash)β for comparing objects by structure (provided objects do not contain floats). lowkey the default choice, but mind the no-floats thing, bestie, that's a real constraint. π((fun x y -> compare x y = 0), MoreLabels.Hashtbl.HashedType.hash)β for comparing objects by structure and handlingnancorrectly. yes this one gets it, this one does the nan thing right. slay. π((==), MoreLabels.Hashtbl.HashedType.hash)β for comparing objects by physical equality (e.g. for mutable or cyclic objects). the "same actual object or nothing" girlie. perfect for when structure comparison would just loop forever. π
β¨ that's the whole page, bestie β every flag, every function, every caveat, nothing skipped β¨
this gem dropped from OCamldoc on 2026-06-22, and the page you just read is MoreLabels.Hashtbl.HashedType(3). take her, love her, keep your hashes constant. mwah. πππ¦
π¦ 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