Skip to content

ocaml preprocessor that generates a recursive module

License

Notifications You must be signed in to change notification settings

giltho/ocaml-ppx_gen_rec

 
 

Repository files navigation

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.

About

ocaml preprocessor that generates a recursive module

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • OCaml 100.0%