# Custom type; key for Core.Hashtbl.create

**URL:** https://discuss.ocaml.org/t/custom-type-key-for-core-hashtbl-create/11909
**Category:** Learning
**Created:** [April 7, 2023, 8:58pm UTC](https://discuss.ocaml.org/t/custom-type-key-for-core-hashtbl-create/11909 "2023-04-07T20:58:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)
#### Post date: [April 7, 2023, 8:58pm UTC](https://discuss.ocaml.org/t/custom-type-key-for-core-hashtbl-create/11909/1 "2023-04-07T20:58:14Z")

</div>

Say we create a custom type:

```auto
type t = A of int | B of string

```

1. What do we need to implement in order to use it as the Key for a Core.Hashtbl ?

2. Is there some ppx that will auto derive this for us ?

Thanks!

---

<div class="post-metadata">

### Author: ![Levi\_Roth](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/levi_roth/32/2268_2.png) [@Levi\_Roth](https://discuss.ocaml.org/u/Levi_Roth)
#### Post date: [April 7, 2023, 9:16pm UTC](https://discuss.ocaml.org/t/custom-type-key-for-core-hashtbl-create/11909/2 "2023-04-07T21:16:18Z")

</div>

This can get slightly complicated as there are a few different ways of working with hashtables in `Core`. Some of these differences are due to choices about which specific operations you want to support (for example, do you plan on serializing the hashtable with `bin_prot`?). And some differences reflect historical differences in approach between `Base` and `Core`, both of which approaches are supported in `Core` currently.

But, assuming you want to use `Core.Hashtbl.create`, you just need `compare`, `sexp_of_t`, and `hash`, i.e. the interface documented [here](https://www.ocaml.org/u/10c36cad8de87998be979a3f329a2a06/base/v0.15.1/doc/Base/Hashtbl/Key/module-type-S/index.html).

To get these via ppx, you can do `[@@deriving hash, compare, sexp_of]`, which depends on `ppx_hash`, `ppx_compare`, and `ppx_sexp_conv` (or just use `ppx_jane` to pick up most of the Jane Street ppxes).

---

<div class="post-metadata">

### Author: ![mooreryan](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mooreryan/32/2843_2.png) [@mooreryan](https://discuss.ocaml.org/u/mooreryan)
#### Post date: [April 8, 2023, 3:13am UTC](https://discuss.ocaml.org/t/custom-type-key-for-core-hashtbl-create/11909/3 "2023-04-08T03:13:27Z")

</div>

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.)

```ocaml
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](https://discuss.ocaml.org/t/motivation-for-ppx-hash/10422/3), and [this](https://dev.realworldocaml.org/maps-and-hashtables.html#satisfying-comparator.s-with-deriving) section of Real World OCaml.

(By the way, let me just callback to your previous question about the [point of deriving sexp](https://discuss.ocaml.org/t/what-is-the-point-of-deriving-sexp/11513/2). I mentioned there about certain of Base/Core functors required sexp, well, above is one example of that!)
