reasonml / reason

Simple, fast & type safe code that leverages the JavaScript & OCaml ecosystems
http://reasonml.github.io
MIT License
10.13k stars 430 forks source link

Syntax sugar for (immutably) modifying js-objects #2467

Open jfrolich opened 5 years ago

jfrolich commented 5 years ago

We have {"key": value} syntax sugar for creating JavaScript-objects. With records we have syntax to immutably update a record:

let meNextYear = {...me, age: me.age + 1};

For JavaScript interop it would be great to have similar syntax for objects, something like:

let meNextYear = {...me, "age": me##age + 1};

This because it's not possible to have a single function to update an object due to the typesystem. And Js.Obj.assign loses type information (see below).

Js.Obj.empty()->Js.Obj.assign(me)->Js.Obj.assign({"age": me##age + 1})

I am not sure if this is something very hard as it might need modification pretty deep inside of the (ocaml) compiler. But it would be great to have a good solution for this, JS-objects are still used in many libraries even if you don't interop with existing JavaScript code.

ozanmakes commented 5 years ago

Check out js_deep_ppx which includes additional conveniences such as .map support.

jfrolich commented 5 years ago

Check out js_deep_ppx which includes additional conveniences such as .map support.

Cool, thanks, I'll check it out!