how to derive to_string methods for data types using ppx

847 views Asked by At

How can I derive to_string methods for data types using ppx with jbuilder?

For instance, I'm trying to use @@deriving show to derive show_* methods for a data type. I have a simple main.ml file that looks like this:

open Core

type foo = Bar | Baz
  [@@deriving show]

let () = printf "%s\n" (show_foo Bar)

I have a jbuild file in the same directory that looks like this:

(jbuild_version 1)

(executables
 ((names (main))
  (libraries (core ppx_deriving))
  (preprocess (pps (ppx_deriving.show)))))

(install
 ((section bin)
  (files ((main.exe as my_foo)))))

When I run jbuilder build, I get the following error:

File "main.ml", line 6, characters 24-32:
Error: Unbound value show_foo

It seems like jbuilder doesn't run the ppx_deriving.show preprocessor, and doesn't generate the show_foo function.

Is my use of @@derive show correct? Do I need to add something to my jbuild file to get it to work correctly? Does ppx_deriving have a problem working with jbuilder? Should I be using a different ppx library? Should I be using a different build system? How do most OCamlers deal with these kind of build system problems?


edit: I am using OCaml version 4.04.2, jbuilder version 1.0+beta11, and ppx_deriving version 4.1.

1

There are 1 answers

0
Étienne Millon On BEST ANSWER

As you found in the comments, this requires particular handling in ppx_deriving which was only merged in version 4.2.

The underlying reason is that jbuilder uses ppx_driver to apply ppx rewriters and not the -ppx flag.