如何更新榆木内部记录 [英] how to update an inner record in elm

查看:44
本文介绍了如何更新榆木内部记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个模型

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

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

在我的更新功能中,如果要更新exampleId,将是这样:

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

 { model | exampleId = updatedValue }

但是,如果我需要更新,例如结果值,该怎么办在GroupExamples内部?

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

推荐答案


用该语言完成此操作的唯一方法是,不破坏外部记录,例如:

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 }

还有焦点包,您可以:

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

这篇关于如何更新榆木内部记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆