How to update nested Records?
Not best practice: Create an opaque type instead.
type alias Movie =
{ title : String
, rating : Int
}
type alias Model =
{ currentMovie : Movie
}
rate : Int -> Model -> Model
rate newRating model =
{ model
| currentMovie =
--{ model.currentMovie | rating = newRating }
Debug.todo "the line above does not compile"
}
Question
How can I update a nested record field?
Answer
First get the nested record, then update it:
let
currentMovie = model.currentMovie
in
{ currentMovie | rating = newRating}
Further reading
📄Article: Updating nested records in Elm by Wouter In t Velt
Last updated
Was this helpful?