package ppx_gen_rec

  1. Overview
  2. Docs
A ppx rewriter that transforms a recursive module expression into a `struct`

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_gen_rec-v2.0.0.tbz
sha256=fe6323e544f6d8a406572d6c8e01283a03f3c826327833ed58960d0ef43d9e59
sha512=23ecf170f0792c28d0535fba43d6ae51b0a063974ba1b729ebf0af3d8a088b31f4166296fa0e457623384f11142c8430bf13525f721dfbabc60688fad9c92ab9

README.md.html

ocaml-ppx_gen_rec

A ppx rewriter that transforms a recursive module expression into a struct.

If you write a recursive module like this:

module rec Foo : sig
  type t = string
end = Foo

The compiler treats it like you wrote:

module rec Foo : sig
  type t = string
end = struct
  type t = string
end

If you try to use ppx_deriving, you get a Undefined_recursive_module exception, because ppx_deriving generates the signature but not the implementation:

module rec Foo : sig
  type t = string [@@deriving show]
end = Foo

(* is like writing *)
module rec Foo : sig
  type t = string
  val show: t -> string
end = struct
  type t = string
  let show _ = raise Undefined_recursive_module
end

Use ppx_gen_rec before ppx_deriving to generate an explicit struct, which will cause ppx_deriving to generate an implementation:

module%gen rec Foo : sig
  type t = string [@@deriving show]
end = Foo

(* becomes... *)
module rec Foo : sig
  type t = string [@@deriving show]
end = struct
  type t = string [@@deriving show]
end

(* which becomes... *)
module rec Foo : sig
  type t = string
  val show: t -> string
end = struct
  type t = string
  let show t = (* show stuff *)
end

Usage

Just use module%gen rec instead of module rec:

module%gen rec Foo : sig
  type t = string [@@deriving show]
end = Foo

License

ocaml-ppx_gen_rec is MIT licensed, as found in the LICENSE file.