Skip to content

Place all derived functions into a functor?  #529

@benbellick

Description

@benbellick

Hello!

I'm working on a Ppxlib deriving extension which transforms types into functions (specifically deriving serialization decoders).

I would like to somehow make all of the generated functions encapsulated into a functor that I can then instantiate with a module.

E.g.

(*In file `file.ml` *)
type a = int [@deriving my_ext]
(* makes `a_fun`*)
type b = int * string [@deriving my_ext]
(* makes `b_fun`*)

(* I somehow want to have the above `a_fun` and `b_fun` *) placed into a functor like `File_my_ext` *)

let My_gen_mod = File_my_ext(<MODULE>)

One possible solution is to make it so that

type a = int [@deriving my_ext]
type b = int * string [@deriving my_ext]

generates code like

module File_my_ext (Input : Sig) = struct end
type a = int [@deriving my_ext]

module File_my_ext (Input : Sig) = struct
  include File_my_ext(Input)
  let a_fun = <FUN>
end

type b = int * string [@deriving my_ext]
module File_my_ext (Input : Sig) = struct
  include File_my_ext(Input)
  let b_fun = <FUN>
end

I believe this would work, though it feels quite clunky as I am having to create all of these intermediary functors and instantiate them along the way. Is there a more natural way to do this?

If its relevant, that top level of my extension consists of

Deriving.add name ~str_type_decl |> Deriving.ignore

for some implementation of str_type_decl

Thanks!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions