# Deriving show not working as expected

**URL:** <https://discuss.ocaml.org/t/deriving-show-not-working-as-expected/9413>\
**Category:** Learning\
**Tags:** ppx\_deriving\
**Created:** [February 25, 2022, 11:15am UTC](https://discuss.ocaml.org/t/deriving-show-not-working-as-expected/9413 "2022-02-25T11:15:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mradke](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mradke/32/3579_2.png) [@mradke](https://discuss.ocaml.org/u/mradke)\
**Post date:** [February 25, 2022, 11:15am UTC](https://discuss.ocaml.org/t/deriving-show-not-working-as-expected/9413/1 "2022-02-25T11:15:20Z")

</div>

I’m a little confused on how `ppx_deriving.show` works. I’m pretty new to OCaml so I might be getting something wrong, but I haven’t been able to figure this out on my own yet.

I have the following code in `deriver.ml`:

```auto
type simple =
    { ok: bool; } [@@deriving show]

let s =
  { ok = true; }
let p =
  print_endline (show s)

```

and the following dune file:

```auto
(executable
 (name deriver)
 (preprocess
  (pps ppx_deriving.show)))

```

Now when I run `dune build` I’ll get the error message `Error: Unbound value show` which is suprising since, having worked with Haskell and Rust, I expected `@@deriving` would provide this function automatically.

What am I missing here?

---

<div class="post-metadata">

**Author:** ![CraigFe](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/craigfe/32/1989_2.png) [@CraigFe](https://discuss.ocaml.org/u/CraigFe)\
**Post date:** [February 25, 2022, 11:26am UTC](https://discuss.ocaml.org/t/deriving-show-not-working-as-expected/9413/2 "2022-02-25T11:26:19Z")

</div>

Hi @mradke. Welcome to OCaml!

The derived `show` function for a type called `simple` will be `show_simple`. PPX derivers in OCaml generally have a convention that the type name appears in the names of generated functions unless the type is called `t` (in which case the name is generally omitted). So:

```auto
type t = ... [@@deriving show]
val show : t -> string

type foo = ... [@@deriving show]
val show_foo : t -> string

```

This helps ensure that the generated functions for different types don’t shadow each other.

For debugging these sorts of problems it’s very helpful to have some way to inspect the generated code. In my own editor environment, I tend to do this by putting the code with `[@@deriving]` inside a wrapper module and then querying the type of that module. If you’re using something like VS Code then I’d guess there is a nicer solution than this by now 🙂 One can also use a script like this: [Simple script for expanding OCaml PPXes (in a readable way) · GitHub](https://gist.github.com/CraigFe/b0f7c65534a90ed4db9ad9ca129b2ed4).

Hope this helps!

Craig.

---

<div class="post-metadata">

**Author:** ![mradke](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/mradke/32/3579_2.png) [@mradke](https://discuss.ocaml.org/u/mradke)\
**Post date:** [February 25, 2022, 12:36pm UTC](https://discuss.ocaml.org/t/deriving-show-not-working-as-expected/9413/3 "2022-02-25T12:36:48Z")

</div>

Thank you so much. ❤  
However my SSCCE was unfortunately neither complete, nor correct, I’m afraid… 🙈

What I ommitted was that I use a project layout where I import a library _in which the deriving takes place_. So I wondered why this isn’t working with my imported module?

It turns out that one also needs to specify the `ppx_deriving.show` inside the libraries dune file, like so:

```auto
(library
 (name my_lib)
 (preprocess
  (pps ppx_deriving.show))) ; I omitted this

```

Now, if you have your type `simple` in a file called `mymodule.ml` you can call it from `main.ml` like so: `print_endline (Mymodule.show_simple s)`.

Thanks again for the pointer! 🙂
