Sharing types that need bin_io in native


#1

We’re trying to share types between native ocaml and bucklescript. Our types use a set of derivers that are easy to share - show, eq, etc. We’ve also the yojson deriver using [@@deriving yojson {optional = true}] which has worked well - the types only appear on in native ocaml as that is the only place we try to call it.

However, we’re having trouble using the bin_io deriver. Does anyone have a strategy for using this? The types in question are in source files that are compiled by both bsb and dune, and we’d like the deriver to be called in dune/native and to be ignored by bsb/bucklescript.

I’m thinking that’s a PPX that can do this, some option or package I’ve missed, or maybe y’all do this sort of thing via atdgen or similar?

Thanks!


#2

We have our type definitions in .atd files, and then use atdgen's -deriving-conv flag to insert the deriving annotations we want only on the native side. If your types are in ocaml/reason source files, then maybe a little search/replace script would work, to add the deriving annotations you need.


#3

That’s helpful, thanks!