Using ppx_deriving.show on a nested type hierarchy

Hello,

I’m trying to use (preprocess (pps ppx_deriving.show)) on a large type hierarchy. I added [@@deriving show] to the root type, but then I need to add it again to the next type and its dependencies. This feels quite daunting, especially when I encounter types defined in the standard library or in third-party dependencies.

Do you have any tips on how to approach this? I need to add it to over 100 types, and I’m not sure if this is a rabbit hole I want to go down.

In my case I have had at most 3 levels of type hierarchies where doing it manually on each type is feasible (as formally, I think, it should be done in each type). I have no experience with larger hierarchies and better fancy ways. But I am writing to tell you about ppx_import which can be useful for types in external libraries, in case you did not know, as you can use it in combination with ppx_deriving (in this link in FAQ there are some ideas of more advanced uses, maybe the odoc-ppx-deriving is also useful). Good luck.

I don’t know what you mean by “type hierarchy”, but I’ve used show on the types of the OCaml AST without any problem. The trick is to first use ppx_import to import the types into a single recursive type declaration, then apply the show to that. Here’s an example: pa_ppx_migrate/pa_ocaml_migrate_parsetree/migrate_510_520.ml at master · camlp5/pa_ppx_migrate · GitHub

That imports from pa_ppx_migrate/pa_ocaml_migrate_parsetree/reorg_ast.ml at master · camlp5/pa_ppx_migrate · GitHub where you can see that the code is importing types from the various versions of the OCaml AST into recursive type declarations, so that in the migrate_510_520 file the recursive group can be imported and then have deriving applied to them.

But really, you can just import types in the order of their definition, and apply show to each in sequence. E.g. in pa_ppx/base/pp_parsetree.ORIG.ml at master · camlp5/pa_ppx · GitHub
you can see that there are a bunch of imports, in sequence, and for each, show is applied.

Note well: this is all done using the Camlp5-based pa_ppx PPX rewriters, not the ppxlib-based rewrites. But the same pattern should work: the pa_ppx-based rewriters are meant to be as close to work-alikes of the ppxlib-based rewrites as possible. So you shouldn’t have any problem adapting what I describe above.