Rescript-compiler: Extending bs.deriving accessors to records?

Created on 3 Oct 2017  路  6Comments  路  Source: rescript-lang/rescript-compiler

It'd be nice if records can benefit from the same accessors generation, like they do for variants. So:

type foo = {temperature: float; b: string} [@@bs.deriving accessors]

let ints = List.map temperature foos

Or something similar. Would be nice if setters are generated too. This might not be the best way to do it, but it might make things like lenses a bit easier.

Alternatively, we can drop this altogether and just use some syntax in Reason for deeply nested record updates...

HIGH

Most helpful comment

I'd like record accessors too, like just make a function of the name of the record that is of arity of however many fields the record has in the order that the record declares them, like:

type foo = {temperature : float; b : string} [@@bs.deriving accessors]

(* generates a function like: *)

let foo temperature b = {temperature; b}

In addition to that, a general set of lenses would be nice, but if we are doing that then we may as well bring in the entire deriving ppx for fields/records/variants and all of the very nice and useful functions that they generate (I'd use so much of them...) instead of just the single one that is generate in BS now. :-)

Although a nicer way to use general PPX's from BSB would be useful too.

All 6 comments

Looking for a less verbose way to do this in Reason

let state' = {...state,
  view: {...state.view,
    boardView: {...state.view.boardView
      loading: state.network.protocol.loadingBoard}}};

Perhaps a special syntax would be the best way to do it?
e.g a nested spread (that compiles to the above)
let state' = {...state.view.boardView.loading: state.network.protocol.loadingBoard};

I'd like record accessors too, like just make a function of the name of the record that is of arity of however many fields the record has in the order that the record declares them, like:

type foo = {temperature : float; b : string} [@@bs.deriving accessors]

(* generates a function like: *)

let foo temperature b = {temperature; b}

In addition to that, a general set of lenses would be nice, but if we are doing that then we may as well bring in the entire deriving ppx for fields/records/variants and all of the very nice and useful functions that they generate (I'd use so much of them...) instead of just the single one that is generate in BS now. :-)

Although a nicer way to use general PPX's from BSB would be useful too.

A constructor function with the same name as the record type would be nice. Accessor functions with the same name as record fields would also be nice--but they would suffer from the same partiality problem as they do in Haskell, once we upgrade to the latest OCaml syntax with embedded record types inside variant constructors.

Of course, the attribute is optional, so it would be a more 'enter at your own peril' nuanced situation in OCaml, but it would introduce complexity.

Lenses are very general constructs, allowing getting, setting, updates, optional updates, mapping, etc. Simple accessors can't cover the full breadth of functionality, I think. It would be good if we could reuse some of the existing work e.g. https://github.com/pdonadeo/ocaml-lens

about lens, I made a feature wish here: https://github.com/facebook/reason/issues/1449

I'm writing some code to diff records -- iterator functions would be very useful in addition to accessor functions: https://github.com/janestreet/ppx_fields_conv

It would be great to have generated setters to simplify lenses creation. Is there any progress on this issue? Can I help somehow?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bobzhang picture bobzhang  路  3Comments

cknitt picture cknitt  路  3Comments

cknitt picture cknitt  路  3Comments

bobzhang picture bobzhang  路  4Comments

TheSpyder picture TheSpyder  路  5Comments