First class fields PPX?


#1

It looks like OCaml native includes a built-in PPX for first class field accessors and setters:
https://dev.realworldocaml.org/records.html#first-class-fields

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 https://github.com/ryb73/ppx_decco by @ryb73 or https://github.com/jaredly/let-anything 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)?


#2

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.


#3

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?


#4

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?


#5

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!


#6

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


#7

I JUST DID!

:dark_sunglasses:

Wow, such documentation. So learning.