How to update nested Records?

Not best practice: Create an opaque type instead.

Problem
Solution
Alternative Solution
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"
}
type alias Movie =
{ title : String
, rating : Int
}
type alias Model =
{ currentMovie : Movie
}
rate : Int -> Model -> Model
rate newRating ({currentMovie} as model) =
{ model
| currentMovie =
{currentMovie | rating = newRating }
}
type alias Movie =
{ title : String
, rating : Int
}
type alias Model =
{ currentMovie : Movie
}
rate : Int -> Model -> Model
rate rating ({currentMovie} as model) =
model
|> updateMovie (addRating rating)
updateMovie : (Movie -> Movie) -> Model -> Model
updateMovie fun ({currentMovie} as model) =
{ model | currentMovie = fun currentMovie}
addRating : Int -> Movie -> Movie
addRating rating movie =
{ movie | rating = rating}

This is the a better solution

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}

For function parameters we can use the keyword as to bind fields of a record to a variable with the same name:

rate newRating ({currentMovie} as model) =
...

Further reading