If you are asking for Hashtbl
you may also be interested in using your custom types as keys for Map
and Set
as well.
Here’s an example of setting your type up so you can use it with either a Hashtbl
, Map
, or Set
. It uses the Comparator.Make
functor so you don’t have to write the boilerplate yourself. (note: you will see this inner-module-T pattern used a lot in base/core, so if you plan to use them, it is a good pattern to be familiar with.)
open! Base
module Foo = struct
module T = struct
type t = int [@@deriving compare, hash, sexp_of]
end
include T
include Comparator.Make (T)
(* Lot's of stuff. *)
end
let foo_ht = Hashtbl.create (module Foo)
let foo_map = Map.empty (module Foo)
let foo_set = Set.empty (module Foo)
You can see more info here: motivation for ppx_hash, and this section of Real World OCaml.
(By the way, let me just callback to your previous question about the point of deriving sexp. I mentioned there about certain of Base/Core functors required sexp, well, above is one example of that!)