Shorthand way for assigning a single field in a record, while copying the rest of the fields?

前端 未结 3 553
陌清茗
陌清茗 2020-12-12 13:04

Let\'s say I have the following record ADT:

data Foo = Bar { a :: Integer, b :: String, c :: String }

I want a function that takes a record

3条回答
  •  不知归路
    2020-12-12 13:50

    You don’t need to define auxiliary functions or employ lenses. Standard Haskell has already what you need. Let’s take the example by Don Stewart:

    data Foo = Foo { a :: Int, b :: Int , c :: String }
    
    test = Foo 1 2 "Hello"
    

    Then you can just say test { c = "Goodbye" } to get an updated record.

提交回复
热议问题