Search code examples
recordpurescript

How to partially update a purescript record


everyone!

I'm a Purescript beginner and have trouble with working on records.

I have one record type:

type Employee =
 { firstName :: String
 , lastName :: String
 , address :: String
 , height :: Number
 , weight :: Number
 ...
 }

And I want to update just a portion of this record. Let's say I want to only update height like the following typescript code.

let a: Employee = {
 ...a,
 height: 180
}

How can I achieve this in Purescript? Thank you.


Solution

  • Syntax for record update in PureScript is the following:

    r2 = r1 { x = 42, y = "foo" }
    

    Where:

    • r1 is the original record
    • r2 is the new, updated record
    • x and y are record fields (not necessarily ALL fields)

    The above snippet is equivalent to the following JavaScript code:

    r2 = { ...r1, x: 42, y: "foo" }