how to update an inner record in elm

匆匆过客 提交于 2019-12-04 03:42:41

问题


I have this model

type alias Model = 
  { exampleId : Int
  , groupOfExamples : GroupExamples
  }

type alias GroupExamples = 
  { groupId : Int
  , results : List String
  }

In my update function, if I want to update the exampleId would be like this:

 { model | exampleId = updatedValue }

But what if I need to do to update, for example, just the results value inside of GroupExamples?


回答1:


The only way to do it in the language without anything extra is to destructure the outer record like:

let
    examples = model.groupOfExamples
    newExamples = { examples | results = [ "whatever" ] }
in
    { model | groupOfExamples = newExamples }

There is also the focus package which would allow you to:

set ( groupOfExamples => results ) [ "whatever" ] model


来源:https://stackoverflow.com/questions/34958667/how-to-update-an-inner-record-in-elm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!