Bs-deriving: Type-driven code-generation for ReasonML — I successfully ported ppx_deriving to BuckleScript!


#1

I proofed the process with bs-sedlex, and after working through a few hiccoughs, I’ve now got ppx_deriving working as well!

It’s on npm as bs-deriving and ppx-deriving (there’s two packages; one is the ML runtime, the other is the precompiled binary ppx-driver.) Read the README intro for details on how to install and use in a BuckleScript project.

tl;dr for those of you who’ve never seen it used in OCaml projects: ppx_deriving generates generic functions and modules given only a type declaration. A simple example is [@deriving show], which given this declaration from the README (converted to the Reason syntax) …

[@deriving show]
type myfile = {
  name: string,

  [@printer fmt => fprintf(fmt, "0o%03o")]
  perm: int,
};

bs-deriving would then generate a show_myfile function:

show_myfile({ name: "dir", perm: 493 });
/* returns the string '{ name = "dir"; perm = 0o755 }' */

This is a super-basic example, and doesn’t begin to cover all the built-in ‘deriving modules’; they can generate custom comparison-functions, mappers, constructors, and so-on.