Deriving make for abstract types in signatures

ppx_deriving
code-generation

#1

Hi! I’ve a code generation question.

I’m using ppx_deriving with the make plugin. It generates a constructor function for record types with fields mapped to labeled/optional arguments. It’s very useful!

The problem is that I need to generate the make function in a signature for an abstract type, which is not supported. Of course, in a normal situation, I would simply write the function declaration manually, but in this case I’m generating OCaml code (as a string) from a Swagger spec.

Is there a way to use the make plugin programatically? Are there any alternative solutions to this problem?

Thanks in advance! :slight_smile:


#2

Probably more than one way to approach this problem. One way might be to generate the record type and its derived constructor into a structure that you then constrain to a more restrictive signature with an abstract type using module coercion or inclusion. Another way might be to generate a signature with a private record type decorated with the deriving attribute.


#3

@jhw Thanks for your suggestions.

I’m not sure if your first suggestion would work as module type constrain would require a signature for the make function which is unknown at the code generation time (the same applies to inclusion I think). Even if it did work, I worry that it would complicate the public interface for users.

The private record type would be ideal in this case. It would allow record field access but that doesn’t seem too problematic.

Thanks again!