First class fields PPX?


It looks like OCaml native includes a built-in PPX for first class field accessors and setters:

This is something that I miss from Elm. I think it’d be really nice to have first class fields in Reason as well. I tried using the [@deriving fields] decorator on a record to see if it works, but it looks like BS doesn’t bundle that PPX, and just ignores the decorator.

Would someone be interested in trying to write a PPX like that on their own? Writing PPXs is a pretty cryptic topic, and IMO really lacking in friendly documentation, but it might be a fun project to take on. Referencing by @ryb73 or by @jaredly might be a good place to start.

Or, @bobzhang @chenglou would you be open to the idea of bundling that PPX with bsb (I don’t know if it’s that easy, just guessing here)?


OCaml doesn’t come with that plugin, it gets supplied via things like Caml5p or ppx_fields_conv or so. Or if you are using bucklescript only with no native compilation then it has a very simplified form of the above PPX in the form of this. I prefer the PPX as it has a lot more type-safe helpers.


Ah, I was under the impression that it shipped with core because of this line from the book:

The ppx_fields_conv syntax extension that ships with Core does just that.

@OvermindDL1 I looked through the link you posted again, and I don’t see a PPX that makes accessor functions for record fields. Am I missing something?


Core is another library, an extension to the standard library. Core does not ship with OCaml, but it is well worth having, it makes the standard library much better in about every way. And yes, ppx_fields_conv is made by the same people.

Which link? The ppx_fields_conv one you do by putting the [@@deriving fields] attribute on the record. For Bucklescript as per the link it is via the [@bs.deriving accessors] attribute?


Ooooh that’s really helpful to know. Thank you!

You just blew my mind. I’ve read those docs so many times, but since they only mention variants in the docs in association with @bs.deriving accessors, I’ve never thought to just try it with records to see if it would work. WOW!


Sounds like you should submit a Documentation Issue Ticket or a PR? ^.^




Wow, such documentation. So learning.