如何更新榆树的内部记录

Mat*_*tos 4 elm

我有这个模型

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

type alias GroupExamples = 
  { groupId : Int
  , results : List String
  }
Run Code Online (Sandbox Code Playgroud)

在我的更新函数中,如果我想更新exampleId将是这样的:

 { model | exampleId = updatedValue }
Run Code Online (Sandbox Code Playgroud)

但是,如果我需要更新,例如,只更新GroupExamples中的结果值,该怎么办?

rob*_*oby 11

在没有任何额外内容的情况下使用该语言的唯一方法是对外部记录进行解构,如:

let
    examples = model.groupOfExamples
    newExamples = { examples | results = [ "whatever" ] }
in
    { model | groupOfExamples = newExamples }
Run Code Online (Sandbox Code Playgroud)

还有一个焦点包,可以让你:

set ( groupOfExamples => results ) [ "whatever" ] model
Run Code Online (Sandbox Code Playgroud)