FYI.
I was not very explicit in my first post, but I believe this update syntax should also be used for Records as well. My suggestion could be summarized that Objects need an ability to safely update, and Records need a smoother ability to safely update.
So instead of this
let updateMe = {...me, name:"bob", favoriteColor:Blue, contact:{...me.contact, address:{...me.contact.address, street: "321 new address"}}}
We just do this:
let updatedMe = update_person(me, (draft) => {
draft.name = "bob"
draft.favoriteColor = Blue
draft.contact.address.street = "321 new address"
})
For a Record setup like below:
type address = {
street:string,
city:string,
zip:int
}
type contact = {
address: address,
phone:string
}
@decorator
type color = Red | Blue | Green
type person = {
name: string,
age: int,
favoriteColor: color,
contact:contact
}
let me: person = {
name: "sean",
age: 21,
favoriteColor: Red,
contact: {address: {street:"123 old address", city:"chicago", zip:1235}, phone: "555-617-5854"}
}
Why donāt you do a JS RFC with that suggestion, see what the feedback is.
1 Like
Yeah, now that I have thought about it more and I can articulate it betterš
Introduction to Immer | Immer provides a JS implementation of the suggested update mechanism. Itās based on the Proxy API.
Introduction to Immer | Immer provides a JS implementation of the suggested update mechanism. Itās based on the Proxy API.
Yeah, Immer is my inspiration. I mocked out the functionality for both Objects and Records. I need to update the functionality to use Proxy. Luckily for ReScript, we donāt need to do so many checks as Immer because the scope is smaller with just Objects and Records.
Would be nice if this was just built into the language, but it should be possible with a PPX as well.
1 Like
Iām quite fond Kotlinās simple syntax for making new objects of data classes:
person.copy(age=21, favoriteColor=Blue)
Something similar with the immer functionality would be a huge DX improvement. I find object spreads in codebases not the easiest to read.
And I know keywords are frowned on, but look how nice it looks when reusing new
let p2 = new person {
age = 21
contact.address = "321 new address"
}
2 Likes